home *** CD-ROM | disk | FTP | other *** search
/ Shareware Universe - The Gold Collection / Shareware Universe The Gold Collection.iso / ray / rayce / rayce.doc < prev    next >
Text File  |  1996-04-19  |  89KB  |  2,731 lines

  1. This file documents the Rayce 2.8 raytracer
  2.  
  3.    Copyright 1993,1994 Han-Wen Nienhuys
  4.  
  5.  
  6.  
  7.  
  8.    This document describes version 2.8 of the Rayce raytracer. It was
  9. made from an info file.
  10.  
  11.  
  12. Copying
  13. ********
  14.  
  15.    SEE THE FILE "COPYING" WHICH IS INCLUDED IN THIS PACKAGE. RAYCE IS
  16. DISTRIBUTED UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE
  17.  
  18.  
  19.  
  20. Introduction
  21. *************
  22.  
  23.    Rayce is a raytracer I've written to find out how raytracing works.
  24. It's written for purely educational purposes.  If you want to make
  25. neat pictures, use PoV or Vivid instead.  Oh, by the way,
  26. "educational" means "for my education", but you could use it in a
  27. C.G.  class too. It wouldn't make a very good example, though, I am
  28. afraid. Most of the design and implementation is my own, our
  29. university library's copy of "An intro to Ray Tracing" hasn't been
  30. available for some time, so I was on my own, while programming Rayce.
  31.  
  32.    Still, Rayce has been expanded very much, and I plan to
  33. incorporate a lot of weird and new stuff into it.
  34.  
  35.    What features does Rayce have?
  36.  
  37.    - Rayce supports these primives:  sphere, quadric, plane,
  38.      lightsource, box, torus, algebraic, superquadric, polygon,
  39.      triangle, disc
  40.  
  41.    - It has these non-primitives: composite, translational extrusion,
  42.      CSG
  43.  
  44.    - Rayce has distribution raytracing for rendering penumbra, motion
  45.      blur, gloss and translucency.
  46.  
  47.    - Rayce has multiple efficiency schemes (well :-): manual bounding
  48.      hierarchies or recursive bintree traversal.
  49.  
  50.    - The sources are GPL-ed, and they're portably written ANSI C.
  51.  
  52.    - Debugging facilities
  53.  
  54.    - A POV like input language
  55.  
  56.    - Imagemaps
  57.  
  58.    What important features are still missing?
  59.  
  60.    - Solid texturing
  61.  
  62.    - Bumpmaps
  63.  
  64.    - Serious anti aliasing.
  65.  
  66.    - Support for these primitives
  67.  
  68.         * Heightfield
  69.  
  70.         * Bezierpatches
  71.  
  72.         * Cones
  73.  
  74.         * Blob
  75.  
  76.  
  77. Credits
  78. ********
  79.  
  80.    Among others, these people have (knowingly or unknowingly)
  81. contributed to Rayce:
  82.  
  83. George Kyriazis <kyriazis@turing.cs.rpi.edu>
  84.      Rayce is based on a very small raytracer by George Kyriazis. 
  85.      You can still download this raytracer on wuarchive.wustl.edu, in
  86.      the directory `/pub/graphics/graphics/ray/gk/' as
  87.      `ray2*.shar.z'.  This raytracer was called Ray, I've changed the
  88.      name to avoid confusion. Besides, "ray" isn't a very original
  89.      name for a raytracer, is it? (and "Raycer" was too obvious :-).
  90.  
  91. Shawn McHorse <zaphod@fcircus.sat.tx.us>
  92.      I would like to especially thank Shawn McHorse for valuable
  93.      comments, bugfixes, the Rayce output in the SPD, the DJGCC
  94.      compile and the new shading code.
  95.  
  96. The POV-team
  97.      In developing, I have used Persistence Of Vision, a great
  98.      raytracer usually dubbed POV, as a shining example. That's why
  99.      it looks internally so much like POV.
  100.  
  101.      The formulas from the `surfs.inc' were taken partly from a POV
  102.      2.0. include file.
  103.  
  104. Mark Vandewettering <markv@cs.uoregon.edu>
  105.      The color include, and the concave polygon code were taken from
  106.      for Mark Vandewettering's MTV tracer. B.T.W. the colors look an
  107.      awful lot like the X11 color database.
  108.  
  109. Ben Trumbore <???>
  110.      Most of the auto bounding for primitives is from: Graphics Gems
  111.      III, Section VI.5, "rectangular bounding volumes for popular
  112.      primitives," by Ben Trumbore, `bounding_volumes.c'
  113.  
  114. David G. Hook, Bernie Kirby and ??? <echidna@munnari.oz.au>
  115.      The Sturm sequence code in `solve.c' was taken from the raytracer
  116.      Art, part of the Vort package (it is almost identical to the
  117.      code in Graphics Gems I), by David G. Hook, Bernie Kirby and
  118.      Peter McAree.
  119.  
  120.      The random numbers are also from Vort.
  121.  
  122. Jochen Schwarze <schwarze@isa.de>
  123.      The cubic and quartic solver was by Jochen Schwarze, and was
  124.      taken from Graphics Gems I.
  125.  
  126. Harm Hanemaayer <hhanemaa@cs.ruu.nl>
  127.      The GIF reader was based on code from a GIF viewer by Harm
  128.      Hanemaayer, and it was based on posting in rec.games.programmer
  129.      in spring of 1993.
  130.  
  131. Roy Hall <roy@wisdom.graphics.cornell.edu>
  132.      The "angle" code in `shade.c' is based on parts of `D.c' from
  133.      Roy Halls book on shading.
  134.  
  135. Keith Bostic <harvard!seismo!keith>
  136.      Keith wrote the public domain `getopt(3)', which is distributed
  137.      for use on non-unix systems
  138.  
  139. ??? Sung <???>
  140.      He wrote the file `bsp.c' in GGems III: VI.1, Sung, "Ray Tracing
  141.      with the BSP Tree", on which Rayce's bsp is based
  142.  
  143.  
  144. Invoking Rayce
  145. ***************
  146.  
  147.    The syntax of Rayce is simple:
  148.  
  149.      rayce [options]
  150.  
  151.    If you don't give options, it will print a small usage screen. 
  152. You can use the following command line options:
  153.  
  154. `-o OUTFILE'
  155.      Specifies the output file, default: `output.tga'. The output
  156.      format is 24 bit type 2 uncompressed Targa.
  157.  
  158. `-i INFILE'
  159.      Specifies the input file, default: `input.r'.
  160.  
  161. `-l LIBPATH'
  162.      Add LIBPATH to the list of library paths. These paths are
  163.      searched for opening input files. Use multiple `-l' arguments
  164.      for multiple paths.
  165.  
  166. `-I #'
  167.      Specifies the number of samples per pixel to use (default: use
  168.      the number In the `options{}' block which is 1 by default). It's
  169.      best to use a square, eg. 4, 16, 25, etc, as other numbers often
  170.      cause. aliasing effects (try `lamp.r' with 5 samples per pixel).
  171.  
  172. `-c'
  173.      Continue a previously aborted trace; default: don't continue.
  174.  
  175. `-x'
  176.      Disable abort via keypress (default: allow abort).  If you have
  177.      compiled Rayce for a different computer than IBM PC running MS
  178.      DOS, the `check_abort()' in your system sourcefile determines
  179.      the abort condition.
  180.  
  181.      E.g. for Linux, this flag has nog effect.  Interrupting should
  182.      be done by sending a SIGINT (pressing Ctrl-C, killing Rayce,
  183.      rebooting, etc.)
  184.  
  185. `-h #'
  186.      Specify image height in pixels, default: 50.
  187.  
  188. `-w #'
  189.      Specify image width in pixels, default: 50.
  190.  
  191. `-d #'
  192.      Turn on debugging.  The number given is the sum of the debug
  193.      flags you want to use:
  194.  
  195.     `1'
  196.           switch on debugging for tokenizer.
  197.  
  198.     `2'
  199.           switch on debugging for parser (`yydebug = 1')
  200.  
  201.     `4'
  202.           switch on runtime debugging messages
  203.  
  204.     `8'
  205.           print the interior (the scene, grids, optimisation data,
  206.           etc.) after parsing
  207.  
  208.     `16'
  209.           print the declarations (declarations, the scene, etc.)
  210.           after parsing
  211.  
  212.      This only works if rayce was compiled with `-DDEBUG'. Default: no
  213.      debugging info.
  214.  
  215. `-t'
  216.      Test flag, sets `test_flag' to `TRUE'.
  217.  
  218.    After a line has been finished, some statistics are printed, and
  219. after Rayce has finished tracing, more elaborate statistics are
  220. printed.
  221.  
  222.  
  223. Input Language
  224. ***************
  225.  
  226.    The input is based on POV 1.0 and 2.0 format.  This chapter is an
  227. overview of the language. If you want the details, check out the
  228. sources, specifically `rayparse.y', the Yacc/Bison file which
  229. implements the parser.  Okay, fasten your seatbelts:
  230.  
  231.  
  232. Basics
  233. =======
  234.  
  235.    Comments are between `/*' and `*/'. Line comments are after a
  236. `//'.  It is legal to nest the `/* */'.
  237.  
  238.    A float is what you would expect it to be:
  239.  
  240.      1.2     12e-1   .12e1
  241.  
  242.    all specify the same number. Additionally, you can use expressions
  243. for floats, these are the allowed operators in order of precedence
  244. from low to high: `+, -, /, *, ^'. An assignment to a variable is
  245. also an expression. Example:
  246.  
  247.      12/10
  248.      0.12*10
  249.      1.0 + 0.2
  250.      1.2^2 - 0.14
  251.      #define a = - (- 1.2)
  252.      #define b = ((a = 2) - .8)
  253.  
  254.    These expressions are all valid, and all specify 1.2.
  255.  
  256.    You can make a vector out of three floats:
  257.  
  258.      <FEXP1, FEXP2, FEXP3>
  259.  
  260.    FEXP1, FEXP2 and FEXP3 are floating point expressions. You can use
  261. expressions on vectors in the same manner as you can with floats:
  262. These are the allowed vector operations: `+,-' do addition and
  263. substraction.  Use `*, /' for scalar multiplies and divisions, `^'
  264. for a vector cross product. From now on, I will use <VECTOR> to
  265. denote a vector. Use
  266.  
  267.      (<VEXP1>, <VEXP2>)
  268.      |<VEXP3>|
  269.  
  270.    for the improduct and the length of a vector respectively; these
  271. are float expressions. The coordinate vectors are `x', `y' and `z'.
  272. Example:
  273.  
  274.      <1, 2/3, 4 * (x, <2, 0, 0>) >
  275.  
  276.    should give `<1, 0.6666, 8>'. Here is one other feature:
  277.  
  278.      <0.5>
  279.  
  280.    this will produce the vector <0.5, 0.5, 0.5>. Although this is not
  281. a customary notation in mathematics, it is very easy for specifying
  282. all kinds of shapes, eg.  boxes, scaling factors, etc.
  283.  
  284.    Please note that everywhere two adjacent floats or vectors should
  285. be separated by a comma.
  286.  
  287.    Keywords should be in lowercase. Everything is case sensitive.
  288. Angles in your input file should be in degrees.
  289.  
  290.    Colors work the POV-way. For reasons of POV-compatibility, a
  291. `filter' part is allowed, but it will be ignored. The components can
  292. have any value, eg. negative values give "dark" colors.
  293.  
  294.    An extra feature is to convert colors from and to vectors. This
  295. makes adding and substracting colors easier.
  296.  
  297.    Example: These all give the same color.
  298.  
  299.      color red 0.5 green 0.1
  300.      color red 0.5 green 0.1 filter 0.5
  301.      color rgb <0.5, 0.1, 0>
  302.      #define testcol = color red 1.0 green 0.2
  303.      color rgb 0.5 * <color testcol>
  304.  
  305.  
  306. Camera
  307. =======
  308.  
  309.    Rayce has a simple pinhole camera, and it is lefthanded, similar to
  310. POV's camera. This camera can't be distorted, though.
  311.  
  312.    The viewpoint is specified by
  313.  
  314.      camera {
  315.        CAMERA_DIRECTIVES
  316.      }
  317.  
  318.    These can be a CAMERA_DIRECTIVE:
  319.  
  320. `location <EYE>'
  321.      EYE is the eye location.
  322.  
  323. `sky <SKYVECTOR>'
  324.      <SKYVECTOR> is the analogon of the POV sky vector.  Your
  325.      pictures will have their the top of your screen aligned to
  326.      <SKYVECTOR>
  327.  
  328. `direction <EYEDIR>'
  329.      <EYEDIR> is the viewing direction. This is not used very often,
  330.      since `look_at' is so much easier.
  331.  
  332. `fov FOV'
  333.      FOV specifies the vertical field of view angle in degrees.
  334.  
  335. `aspect ASPRATIO'
  336.      ASPRATIO is the aspect ratio of your picture (normal computer
  337.      screens have aspect 4/3 = 1.333)
  338.  
  339. `look_at <POINT>'
  340.      `look_at' points the camera to towards <POINT>, while preserving
  341.      the direction vector length (so the field is also preserved)
  342.  
  343.    Hope you understand this... A camera can be scaled, rotated and
  344. translated.
  345.  
  346.  
  347. Options
  348. ========
  349.  
  350.    General options can be set from the opions block:
  351.  
  352.      options {
  353.        OPTION_DIRECTIVES
  354.      }
  355.  
  356.    These can be used as OPTION_DIRECTIVE:
  357.  
  358. `time START, STOP'
  359.      START and STOP specify the time interval in which the scene
  360.      takes place: this is used with the `speed' vector from the object
  361.      declaration. Default: START,STOP = 0,0.
  362.  
  363. `background [<GRADIENT>] color BACKGROUNDCOLOR'
  364.      <GRADIENT> specifies in which direction the gradient of the
  365.      background color should be, and BACKGROUNDCOLOR is the color to
  366.      use.  If you don't want the gradient coloring, then use
  367.  
  368.           background color BACKGROUNDCOLOR.
  369.  
  370. `depth DEPTH'
  371.      DEPTH is the maximum recursion depth. Larger values will result
  372.      in lengthier computations and more accurate results. Default: 3
  373.  
  374. `iterations NUMBEROFFRAMES'
  375.      NUMBEROFFRAMES is the number of eyerays per pixel.  If you want
  376.      to use antialiasing, set iterations to a value greater than 1. 
  377.      This should also be done if you want to use distribution
  378.      raytracing (If you are not familiar with this technique *note
  379.      Distribution Raytracing::.). This value can be overridden by the
  380.      `-I' command line option. Default: 1
  381.  
  382. `tolerance SHADOW_TOLERANCE'
  383.      SHADOW_TOLERANCE is the minimum intersection distance: any
  384.      intersection that is closer than SHADOW_TOLERANCE to the ray
  385.      origin is ignored.  By default it is 1e-3, and with simple
  386.      shapes, you could make it as small as 1e-10.  With certain
  387.      shapes (notably high order surfaces, such as toruses), this
  388.      needs to be bigger, otherwise numerical inaccuracies will find
  389.      the intersection at x = t*dir + pos with t smaller than TOL. 
  390.      These are the intersections that should have t = 0.  These
  391.      intersections cause "phantom" shadows and digital zits. Default:
  392.      0.001. Larger values reduce fantom shapes, but they can
  393.      eliminate correct shadows. The tolerance should be less the 1.0
  394.      (eg. 0.1)
  395.  
  396. `antialias FILTERTYPE'
  397.      Contributions of sampled rays are modulated with a FILTERTYPE.
  398.      filter.
  399.     `0'
  400.           uniform filter
  401.  
  402.     `1'
  403.           Gaussian filter
  404.  
  405. `cutoff TRESHOLD'
  406.      Adaptive depth control is done by specifying a cutoff THRESHOLD.
  407.      If a ray would contribute less than a TRESHOLD fraction of the
  408.      color written to the output, then the ray is not traced.  This
  409.      is called adaptive depth cutoff or adaptive depth control.
  410.      Default: 0.0. Larger values will speed up rendering of scenes
  411.      with a lot of refraction and reflection.
  412.  
  413. `bunching BUNCHFACTOR'
  414.      BUNCHFACTOR is the maximum number of objects that Rayce tries to
  415.      stuff in a voxel or bounding volume if automatic efficiency is
  416.      enabled. Default: 10
  417.  
  418. `optimisation OPTITYPE'
  419.      Optimisation type Default: BSP:
  420.  
  421.     `1'
  422.           manual optimisation
  423.  
  424.     `2'
  425.           BSP
  426.  
  427. Atmosphere
  428. ----------
  429.  
  430.      atmosphere {
  431.        ATMDIRECTIVES
  432.      }
  433.  
  434.    Here,  ATMDIRECTIVES is one of the following:
  435.  
  436. `ior IOR'
  437.      Index of refraction of the atmosphere. Default: 1.0
  438.  
  439. `ambient GLOBALAMBCOLOR'
  440.      Global ambient color. Turn this up to make the whole scene
  441.      brighter. Default: white
  442.  
  443.  
  444. Primitive Shapes
  445. =================
  446.  
  447.    A primitive is a shape that is built into Rayce by the programmer.
  448. At the moment, Rayce supports the following primitives:
  449.  
  450. Sphere
  451. ------
  452.  
  453.    A sphere specified this way:
  454.  
  455.      sphere { <CENTER>, RADIUS }
  456.  
  457.    Here, CENTER is a vector, and RADIUS a float.  The sphere is a
  458. special kind of quadric, and it is somewhat faster than a normal
  459. quadric, see `speedtst.r' for some info.
  460.  
  461.    You might expect a better performance of the sphere in comparison
  462. to the quadric.  I guess, that's because I didn't use vector
  463. operations in `quadric.c'; instead I directly calculated the
  464. quadratic equation in t. See `quadric.c' if you don't understand what
  465. I just typed.  (or you might just as well forget it :-)
  466.  
  467.    A sphere can be used in CSG; the inside of a sphere consists of the
  468. points X with:
  469.  
  470.      distance(X, CENTER) < RADIUS
  471.  
  472. Polygon
  473. -------
  474.  
  475.    You can make polygons in this way:
  476.  
  477.      polygon { <P1>, <P2>, ... }
  478.  
  479.    <P1>, <P2>, ... are the vertices of the polygon. Convex polygons
  480. will render a lot faster.  Polygons do not have an inside or outside,
  481. and they're not closed, therefore a polygon cannot be used in CSG.
  482. The points should be in a plane.
  483.  
  484. Cylinder
  485. --------
  486.  
  487.    You can make a cylinder between <P1> and <P2> having radius R by
  488. inserting
  489.  
  490.      cylinder  { <P1>, <P2>, R }
  491.  
  492.    This isn't really a primitive. If Rayce encounters a cylinder
  493. definition in its input, then it is converted to an extrusion of a
  494. circle with radius R.
  495.  
  496.    A cylinder can be used in CSG.
  497.  
  498. Quadric
  499. -------
  500.  
  501.    A quadric shape is a surface, which is described in this way: it
  502. is a set Q, with:
  503.  
  504.      Q = { (x, y, z) in R^3 | Phi(x, y, z) = 0 }
  505.  
  506.    In which Phi is
  507.  
  508.      Phi(x, y, z) = ax^2 + by^2 + cz^2 + dxy + eyz + fzx + gx + hy + iz + j = 0
  509.  
  510.    or, more casually said: a quadric consists of points x, y, z for
  511. which
  512.  
  513.      ax^2 + by^2 + cz^2 + dxy + eyz + fzx + gx + hy + iz + j = 0
  514.  
  515.    Therefore a quadric is a special kind of algebraic surface.
  516.  
  517.    If you'd take a = b = c =1, j = -1, and d, e, f, ... = 0, Q forms a
  518. sphere, with its center in (0, 0, 0), and radius 1.0.  In general, the
  519. quadric is specified in same fashion as in POV:
  520.  
  521.      quadric { <COEFF1>, <COEFF2>, <COEFF>, CONST }
  522.  
  523.    The coefficients (a, b, c) form <COEFF1>, (c, d, e) <COEFF2>, (f,
  524. g, h) <COEFF3>, all of which are vectors.  The constant j is `const'.
  525. You could also use an `algebraic' for specifying a quadric, but this
  526. is faster.
  527.  
  528.    Quadrics can be used as CSG element.  For a quadric, the inside
  529. are points (x, y, z) with
  530.  
  531.      Phi(x, y, z) < 0.
  532.  
  533. Plane
  534. -----
  535.  
  536.    The plane is a shape mathematically defined by
  537.  
  538.      P = { X in R^3 | (X,n) = a }
  539.  
  540.    Here, (X,n) denotes the dot vector product of the normal n, and a
  541. point X on the plane.  In Rayce and POV it is usually defined by
  542.  
  543.      plane { <NORMAL>, A }
  544.  
  545.    NORMAL is the normalvector to the plane, and A the distance in
  546. which it is moved along the normalvector.  Note that the normal isn't
  547. normalised (which sounds quite funny if you read it aloud :-) The
  548. reason for this is that it isn't normalised in POV either. For the
  549. best results, always use a vector with length 1.
  550.  
  551.    A plane can be used in CSG. A plane is a closed, infinite shape.
  552. It has an outside and an inside.  The outside of a plane is the part
  553. which is on the "side of the normal".  symbolically: X is inside the
  554. plane if
  555.  
  556.      (X,<NORMAL>) < A
  557.  
  558.    This means, that both the inside and the outside are infinite.
  559.  
  560. Box
  561. ---
  562.  
  563.    A box is CSG intersection of six planes. You should specify it
  564. using
  565.  
  566.      box { <POINT>, <OPPOSITE_POINT> }
  567.  
  568.    both POINT and OPPOSITE_POINT are vectors which correspond to two
  569. opposite corners of the box you want Rayce to produce.  The box has
  570. its planes along the X, Y and Z axes.  The order of the coordinates
  571. doesn't matter, so
  572.  
  573.      box { <1, 2, 3 >, <4, 5, 6> }
  574.  
  575.    and
  576.  
  577.      box { <1, 5, 3>, <4, 2, 6> }
  578.  
  579.    should look the same.  A box can be transformed in the usual ways.
  580. Rotating it will slow down tracing a bit, though.
  581.  
  582.    A box can be used in CSG. A box has a proper inside and outside.
  583. Its inside is collection of points (x, y, z) which satisfy
  584.  
  585.      Min.X < x < Max.X
  586.      Min.Y < y < Max.Y
  587.      Min.Z < z < Max.Z
  588.  
  589. Light source
  590. ------------
  591.  
  592.    Light sources are the objects that cause diffuse and specular
  593. illumination, the things that make those nice shadows and highlights.
  594. Use them in this way:
  595.  
  596.      light_source {
  597.        <ORIGIN>
  598.        [LIGHT_MODIFIERS]
  599.      }
  600.  
  601.    This is a point lightsource.  The ORIGIN is the mandatory part: it
  602. tells Rayce the point from which lightrays emanate (actually, shadow
  603. rays are directed to the light source, but you knew that, right?).
  604. Light sources in Rayce cannot be seen. If you want to visualize them,
  605. enclose them with a shape which has the `no_shadow' flag.
  606.  
  607.      light_source {
  608.        direction <DIR>
  609.        [LIGHT_MODIFIERS]
  610.      }
  611.  
  612.    This is a lightsource which casts light in one direction, <DIR>,
  613. and which is "infinitely far away". It doesn't cast any shadows.
  614.  
  615.    These are the allowed `light_modifiers':
  616.  
  617. `color LIGHTCOLOR'
  618.      This tells Rayce to give the light source the color LIGHTCOLOR.
  619.      Default: white
  620.  
  621. `fogsource'
  622.      One can produce fog emanating from one point with `fogsource'.
  623.      The effect looks like a lightbulb in the fog. Default: not a
  624.      fogsource
  625.  
  626. `direction <DIR>'
  627.      Sets the direction of  a lightsource to <DIR>.
  628.  
  629. `point_at <TARGET>'
  630.      This will point lightsources to <TARGET>. ie. direction DIR is
  631.      adjusted to point from <ORIGIN> to <TARGET>.
  632.  
  633. `tightness TIGHT'
  634.      Controls the tightness of fogsources and spotlights. (default:
  635.      0, no directional effects)
  636.  
  637. `no_shadow'
  638.      The lightsource doesn't cast shadows, but the highlights and
  639.      diffuse illumination remain visible.
  640.  
  641. `attenuation FALLOFFEXPONENT'
  642.      FALLOFFEXPONENT is the exponent to use for fading light: in real
  643.      life, the intensity of light diminishes with the distance; the
  644.      intensity is proportional to 1/r^2, so in real life the
  645.      FALLOFFEXPONENT is 2. Light in Rayce will diminsh with
  646.      1/r^FALLOFFEXPONENT.  By default it is 0, i.e. the intensity
  647.      does not depend on the distance from the light source.
  648.  
  649. `radius RADIUS'
  650.      Rayce can do penumbra (soft shadows) as well. Use
  651.  
  652.           radius RADIUS
  653.  
  654.      to specify the radius of a discshaped lightsource. (default:
  655.      radius is 0, no penumbra). Don't forget to turn up the number of
  656.      rays per pixel.
  657.  
  658.    If you're interested: the intensity at an intersection point is
  659. calculated with
  660.  
  661.      (lightdir, DIR)^TIGHT * (1/r)^FALLOFFEXPONENT
  662.  
  663.    Here r denotes the distance to the light source, (,) is the dot
  664. product. Lightdir is the vector from the intersection point to the
  665. lightsource.
  666.  
  667.    Lightsources cannot be used as part of a CSG. For POV compatibility
  668. `spotlight' is also allowed, but it doesn't have any effect.
  669.  
  670. Triangle
  671. --------
  672.  
  673.    A triangle is specified this way:
  674.  
  675.      triangle { <P1>, <P2>, <P3> }
  676.  
  677.    P1, P2 and P3 are the vertices of the triangle, and the order in
  678. which the points are specified does not matter.  No backface culling
  679. is done, and these are ordinary triangles, i.e.  normals aren't
  680. interpolated, if you want that, use smooth triangles. A triangle
  681. cannot be used in CSG.
  682.  
  683. Smooth triangle.
  684. ----------------
  685.  
  686.    A smooth triangle (a.k.a. triangular patch) with interpolated
  687. normal vectors is represented in this way:
  688.  
  689.      smooth_triangle { <P1>, <N1>, <P2>, <N2>, <P3>, <N3> }
  690.  
  691.    <P_I> denote the vertices, and <N_I> denote the normals at these
  692. vertices.  A smooth triangle cannot be used in CSG.
  693.  
  694. Torus
  695. -----
  696.  
  697.    A torus is a shape that looks like a donut.  A torus is specified
  698. like this:
  699.  
  700.      torus { MAJOR, MINOR }
  701.  
  702.    MAJOR is the major radius, and MINOR is the minor radius.  The
  703. torus produced lies in the XZ plane, so you have to rotate it if you
  704. want another direction.  Technically speaking, a torus is a circle in
  705. the XZ plane with radius MINOR and center <MAJOR, 0, 0>, swept around
  706. the Y axis.
  707.  
  708.    Don't try to use `bounded_by' on toruses.  The torus is already
  709. enclosed in an intersection of a minimal bounding sphere and two
  710. planes.
  711.  
  712.    A torus is a quartic surface.  Among others, this means that a
  713. torus can at most have 4 intersections with a ray.  For calculating
  714. these intersections, by default, a technique known as Ferrari's
  715. method is used. Although this technique is relatively fast, it can
  716. give surface acne because of numerical inaccuracies.  The acne is
  717. sometimes called "digital zits", black dots -- try a zoomed in
  718. version of the `papercl.r' with `tolerance 1e-5', and no `sturm'
  719. keywords.
  720.  
  721.    If this happens to you, then try adding the keyword `sturm' after
  722. the definition.  A different method --Sturm sequences-- for
  723. calculating intersections will be used, which is slower, but more
  724. accurate. Alternatively you can set SHADOW_TOLERANCE *Note Options::
  725. higher, eg 0.05. This won't make the calculations more exact, but it
  726. will take away most of the phantom shadows. Don't make tolerance too
  727. big. It should be smaller than one.
  728.  
  729.    A torus can be used in CSG. The inside of a torus (donut) is the
  730. part where the jelly and dough usually resides. If MINOR > MAJOR,
  731. then weird things will happen. Check out the source.
  732.  
  733. High order surfaces.
  734. --------------------
  735.  
  736.    You can model arbitrary algebraic (polynomial) surfaces using
  737. Rayce. Anything is allowed, as long as the order is less than 12 (if
  738. you'd like more, then you'll have to recompile; change `MAX_ORDER' in
  739. `ray.h' to suit your needs).
  740.  
  741.    It is specified with
  742.  
  743.      algebraic {
  744.        $ PHI $
  745.        [ALG_MODIFIERS]
  746.      }
  747.  
  748.    The algebraic surface S is determined by a polynomial Phi. The
  749. surface is the set S for which
  750.  
  751.      S = { (x, y, z) | Phi(x, y, z) = 0 }
  752.  
  753.    In Rayce you get such a surface by defining Phi(x, y, z) by PHI,
  754. or specifying
  755.  
  756.      algebraic { $ PSI(X, Y, Z) = CHI(X, Y, Z) $ }
  757.  
  758.    Which means that
  759.  
  760.      Phi := Psi - Chi.
  761.  
  762.    so Phi is a polynomial in x, y and z. A point is inside the
  763. algebraic if Phi(x, y, z) < 0.
  764.  
  765.    You can perform the following operations on x, y and z to form new
  766. polynomials:
  767.  
  768.      addition, eg. x + y + 83
  769.      substraction, eg. x - z - 83
  770.      exponentation, eg. y ^ 4
  771.      multiplication, eg. 5 * x * y
  772.      grouping, eg. (x+y)^2
  773.  
  774.    Notice that only floating point numbers are allowed, and no
  775. expressions. Example:
  776.  
  777.      $ 1 + 2 $       // ok, we're adding the polynomials 1 and 2
  778.      $ (x, y) $      // WRONG, (x, y) is a float-expression not a
  779.                      // float
  780.      $ 1*x   $       // ok
  781.      $ 1/2   $       // WRONG, you can't divide polynomials (not
  782.                      // even 1 and 2
  783.  
  784.    These are the allowed ALG_MODIFIERS:
  785.  
  786. `closed'
  787.      This keyword is for speeding up inside/outside calculations. Use
  788.      it on closed and finite surfaces. Default: not finite and closed
  789.  
  790. `sturm'
  791.      Use the accurate yet slow sturm sequence solver.  (default:
  792.      rootfinder is the ordinary Ferrari rootfinder)
  793.  
  794.    If you insist on using expressions, then use defined floating point
  795. constants. Example:
  796.  
  797.      #define R = 1.0
  798.      #define r = R/2
  799.      #define mytorus = algebraic {
  800.        $ (x^2 + y^2 + z^2 + R^2 - r^2)^2 = 4*R*(x^2+y^2) $
  801.        closed
  802.      }
  803.  
  804.    In the above, mytorus is defined as a torus with minor radius 0.5,
  805. and major radius 1.0.
  806.  
  807.    Example:
  808.  
  809.      algebraic { $ x^4 + y^4 + z^4 -1 $ closed sturm }
  810.  
  811.    This should specify a closed and finite surface (in this case a
  812. superquadric; a cube with rounded corners), and it is to be calculated
  813. using Sturm sequences.
  814.  
  815.    Since an algebraic is stored in terms of operations,
  816.  
  817.      algebraic { $ (x-1)^2 + y^2 - 1 $ }
  818.  
  819.    is calculated differently from
  820.  
  821.      algebraic { $ (x^2 -2*x + 1) + y^2 - 1.5 + 0.5 $ }
  822.  
  823.    The latter requires more operations, and thus it is more
  824. expensive. Try to make your formula as compact and efficient as
  825. possible.
  826.  
  827.    An algebraic can be used as a CSG part. The inside is defined by
  828. (x,y,z) for which
  829.  
  830.      Phi(x,y,z) < 0
  831.  
  832. Superquadric
  833. ------------
  834.  
  835.    A superquadric is a generalized sphere. For more information,
  836. consult `superq.tex'. It looks like
  837.  
  838.      |x|^P + |y|^P + |z|^P = 1
  839.  
  840.    if p is large, then this forms a cube, with faces parallel to the
  841. axes and rounded corners.
  842.  
  843.    if p = 1, it's a cube with corners in <0,0,1>, <1,0,0>, <-1,0,0>,
  844. etc.
  845.  
  846.    if p = 2, it's a sphere.
  847.  
  848.    This is how you coerce Rayce in rendering one:
  849.  
  850.      superq { <P> }
  851.  
  852.    NB. If P < 1, then you still get a shape, but since the shape then
  853. is not convex anymore, and the intersection algorithm used won't work
  854. reliably; You probably won't get a good render, if you try:
  855.  
  856.      superq { <0.5> }
  857.  
  858.    In Rayce the above is generalized. The algorithm for computing
  859. intersections with superquadrics works for arbitrary shapes
  860.  
  861.      C1(x) + C2(y) + C3(z) = 1
  862.  
  863.    If C1, C2 and C3 are convex functions. So in Rayce, you can also
  864. render elements from this class of shapes:
  865.  
  866.      |x|^P1 + |y|^P2 + |z|^P3 = 1
  867.  
  868.    where P1, P2 and P3 are all greater than 1. This is how you
  869. specify it.
  870.  
  871.      superq { <P1, P2, P3> }
  872.  
  873.    A superq can be used in CSG.
  874.  
  875. Discs
  876. -----
  877.  
  878.    Discs are round shaped pieces of a flat planes, they could be
  879. simulated with a plane `clipped_by' a sphere, but they have been put
  880. in on special request from Shawn McHorse.  Syntax:
  881.  
  882.      disc { <CENTER>, <NORMAL>, RAD }
  883.  
  884.    The above is a simple disc
  885.  
  886.      disc { <CENTER>, <NORMAL>, MAJRAD, MINRAD }
  887.  
  888.    This is a disc with a hole in the center.  A disc cannot be used
  889. in CSG.
  890.  
  891. Transformations
  892. ---------------
  893.  
  894.    * All primitives may transformed using `scale', `rotate' and
  895.      `translate'. Example:
  896.  
  897.           sphere {
  898.             <0>, 1          // now it's a sphere
  899.             scale <1,2,1>   // now it's an ellipsoid
  900.             translate x
  901.             rotate 90*x
  902.           }
  903.  
  904.    * Every shape can have its own private texture.
  905.  
  906.    * Every shape which has a proper inside and outside defined, can be
  907.      inverted using `inverse'.
  908.  
  909.  
  910. Non-Primitive Shapes
  911. =====================
  912.  
  913.    Non-Primitive shapes are shapes, that can be used to form new
  914. shapes from existing shapes. Currently these are supported:
  915.  
  916. CSG
  917. ---
  918.  
  919.    CSG is a term coined for a very useful modelling tool. CSG
  920. --Constructive Solid Geometry-- enables you to add and subtract
  921. objects, thus forming new objects.
  922.  
  923.    These are the shapes that can be used as a CSG part:
  924.  
  925.      sphere
  926.      box
  927.      torus
  928.      extrusion (make sure it's closed! )
  929.      cylinder (idem ditto)
  930.      conic (idem ditto)
  931.      superquadric
  932.  
  933.    The above shapes all have finite (bounded) insides.  In jargon,
  934. you'd say that they are manifolds. Although the following aren't
  935. necessarily finite, you can still use them in CSG as they are closed.
  936.  
  937.      quadric
  938.      plane
  939.      intersection
  940.      union
  941.      algebraic
  942.  
  943.    Generally: any closed shape can be used as a csg element. You'll be
  944. warned if you try to use a non-closed shape.
  945.  
  946.    The above mentioned surfaces (sphere, quadric etc.)  have the
  947. following two properties
  948.  
  949.    Firstly, they divide the whole of 3D space into two distinct pieces
  950. (sets): an "inside" and an "outside".  Every point lies either inside
  951. or outside the shape.  Theoretically, a point could be on the
  952. frontier of the inside and outside, i.e.  on the surface itself.  Due
  953. to numerical inaccuracies and implementation, this rarely happens.
  954.  
  955. Of every ray tested agains a CSG, only
  956.      origin + 2*tolerance * direction
  957.  
  958. is tested for inside/outside.  So if you want to create quirky images,
  959. make sure you have intersections with CSG parts at distances d with
  960.      tolerance < d < 2*tolerance.
  961.  
  962. (So be warned! :-)
  963.  
  964. Some special primitives, like the torus or the sphere, don't need this
  965. testing: if a ray intersects the shape an even number of times, then the
  966. rays origin lies outside the shape, if the ray has an odd number of
  967. intersections, then its origin is inside.
  968.  
  969.    Secondly, one would walk along a ray, a change inside/outside of a
  970. shape happens if and only if the ray hits the surface. The surface is
  971. "closed".
  972.  
  973.    Other CSG algorithms can also use triangles and discs for CSG, but
  974. this one cannot, because a triangle fails the second property.
  975.  
  976.    WARNING: try to avoid CSG wherever possible. Usually,a composite
  977. instead of a union will also do. In that case use the composite, not
  978. the union, a composite is really faster, and it is optimised in
  979. automatic efficiency.
  980.  
  981. CSG Union
  982. ---------
  983.  
  984.    Union is a CSG operation, it merges two shapes into one. A point is
  985. contained in the inside of a CSG union of S1, S2, ... if and only if
  986. the point is inside at least one of S1, S2, ... Syntax:
  987.  
  988.      union {
  989.        CSG_ITEMS
  990.      }
  991.  
  992.    Note that Rayce has a real union.  That is, if you do
  993.  
  994.      union {
  995.        sphere { <0, 0, 0>, 1 }
  996.        sphere { <0, 0, 1>, 1 }
  997.      }
  998.  
  999.    the interior walls are removed: if you are standing in the the
  1000. first sphere, facing the second sphere, you can actually look inside
  1001. the second sphere.
  1002.  
  1003. CSG Intersection
  1004. ----------------
  1005.  
  1006.    Intersection is another CSG operation.  If you do an intersection
  1007. of N shapes, then only points that are contained in all N shapes
  1008. simultaneously, are part of the intersection. So a point is inside a
  1009. CSG intersection of shapes S1, S2, ... if and only if the point is
  1010. inside S1, and S2, and S3 and ....
  1011.  
  1012.    Actually, you can only see the boundary of this intersection.  An
  1013. intersection looks like this:
  1014.  
  1015.      intersection {
  1016.        CSG_ITEMS
  1017.      }
  1018.  
  1019.    CSG_ITEMS is one of
  1020. `shape'
  1021.      One of the allowed CSG shapes.
  1022.  
  1023. `inverse'
  1024.      This flips a shape inside out.  Check out the mathematics
  1025.      section for details.
  1026.  
  1027.      The `inverse' keyword flips a shape: the inside of
  1028.  
  1029.           #define B = shape { A inverse }
  1030.  
  1031.      has all points which are outside of A, and the inside of B has
  1032.      all points which are outside of A.  Mathematically speaking,
  1033.      taking the inverse of a shape, is equivalent to taking the
  1034.      complement of the set, formed by a shape.
  1035.  
  1036. `transformation'
  1037.      It is legal to transform CSGs with `rotate', `scale' and
  1038.      `translate'.  In effect, you are then transforming all components
  1039.      of the CSG, as far as they have been specified.
  1040.  
  1041. `texture block'
  1042.      Adding CSG a texture means the children of the CSG inherit this
  1043.      texture if they don't already have one.
  1044.  
  1045. Extrusion
  1046. ---------
  1047.  
  1048.    You can extrude 2d curves with Rayce: Say, you'd want to make a
  1049. column for use in a roman temple. One option is to use a cylinder
  1050. like this:
  1051.  
  1052.      define column  = object { Disk_Z scale <1, 1, 20> }
  1053.         // now using CSG
  1054.  
  1055.    But you could also regard a cylinder as a circle in the XY plane
  1056. that is that is "thickened" along the Z axis. A circle in the XY
  1057. plane is the intersection curve of a sphere and the XY plane. So you
  1058. could also use:
  1059.  
  1060.      define column  = extrusion { 20, sphere { <0>, 1 } }
  1061.  
  1062.    In general, the syntax of an extrusion is:
  1063.  
  1064.      extrusion {
  1065.        HEIGHT
  1066.        shape { ... }
  1067.      }
  1068.  
  1069.    This takes a shape, and extends the intersection of the shape with
  1070. the XY plane to the Z-direction. Your extrusion will have height
  1071. HEIGHT.  HEIGHT should be bigger than 0. For instance, if you have
  1072. defined the projection of a cogwheel on the XY plane, then you could
  1073. make a 3d object out of it by specifying
  1074.  
  1075.      extrusion {
  1076.        1 intersection { cogwheel }
  1077.      }
  1078.  
  1079.    NB. This is not exactly the same as Polyray's sweep. Polyray
  1080. extrudes polygonal curves or spline curves, and you can vary the
  1081. direction of the extrusion in Polyray.
  1082.  
  1083. Conic extrusion
  1084. ---------------
  1085.  
  1086.      conic {
  1087.        <APEX>, HEIGHT SHAPE { }
  1088.      }
  1089.  
  1090.    Conic extrusion of SHAPE, with apex in <APEX>, height HEIGHT,
  1091. clipping planes parallel to XY plane. BETA! DOES NOT WORK!
  1092.  
  1093. Transformations
  1094. ---------------
  1095.  
  1096.    * You can transform a nonprimitive shape using `translate',
  1097.      `rotate', and `scale': the composite is transformed.  This means
  1098.      that its bounding shape is transformed, and its objects and
  1099.      composites are transformed.
  1100.  
  1101.    * Translation, rotation and scaling will only affect a CSG as far
  1102.      as it has been specified. For more information, look at the
  1103.      definition of hexagon in `shapes.inc'.
  1104.  
  1105.    * All nonprimitive shapes (composites, CSG) can have their own
  1106.      texture.
  1107.  
  1108.  
  1109. Textures
  1110. =========
  1111.  
  1112.    Rayce's textures have more freedom than POV's: every surface
  1113. characteristic can have its own color.  Although this isn't realistic,
  1114. it can give nice effects.  I haven't implemented procedural textures
  1115. yet, so checker- or marbletextures are not possible. For now, use an
  1116. imagemap.
  1117.  
  1118.    Textures have the following syntax:
  1119.  
  1120.      texture {
  1121.        [texture_modifiers]
  1122.      }
  1123.  
  1124.    A `texture_modifier' is one of the following:
  1125.  
  1126. `ambient AMBCOLOR'
  1127.      AMBCOLOR will be the ambient color. Default: black.
  1128.  
  1129. `diffuse DIFFCOLOR'
  1130.      The diffuse color is set to DIFFCOLOR. Default: black.
  1131.  
  1132. `specular SPECCOLOR'
  1133.      The color of a shiny highlight is set to SPECCOLOR.  In POV this
  1134.      is always white.  Default: black.
  1135.  
  1136. `microfacet DISTRIBUTION'
  1137.      Rayce has different microfacet distributions built in. At the
  1138.      moment the following are supported. The syntax has been copied
  1139.      of Polyray, but the keywords have been defined in `standard.inc'.
  1140.  
  1141.     `Phong'
  1142.           Phong is the default distribution. Although it is said to
  1143.           not realistic, it a fairly common distribution. It usually
  1144.           gives a platic like appearance.
  1145.  
  1146.     `Blinn'
  1147.           POV's roughness, a distribution function similar to Phong's
  1148.  
  1149.     `Cook'
  1150.           (a.k.a. Beckmann)
  1151.  
  1152.     `Reitz'
  1153.           (Trowbridge)
  1154.  
  1155.     `Gaussian'
  1156.           The normal (from statistics known) distribution
  1157.  
  1158.      The tightness of the highlight is derived from the roughness of
  1159.      the surface. It can be set `roughness'.
  1160.  
  1161. `roughness ROUGHNESS'
  1162.      It defaults to 0.0. (very small highlight, can't be seen) You can
  1163.      adjust this manually, or you can use `angle'. The rougher a
  1164.      surface, the bigger the "hot spot".
  1165.  
  1166. `angle ANGLE'
  1167.      `roughness' will be set, to produce a highligh that has 0.5 of
  1168.      its maximum intensity at an angle of ANGLE. Smaller values for
  1169.      ANGLE produce tighter highlights.
  1170.  
  1171. `reflection REFLCOLOR'
  1172.      makes the surface reflect an AMOUNT part of the light, and it
  1173.      uses a REFLCOLOR filter.  default:  no reflection, black
  1174.  
  1175. `refraction REFRCOLOR'
  1176.      makes the surface refract a REFRCOLOR part of the light. 
  1177.      Default: black, no reflection.
  1178.  
  1179. `ior IOR'
  1180.      IOR is the material's index of refraction. Default: 1.0
  1181.  
  1182. `refract_angle ANGLE'
  1183.      ANGLE is the maximum deviation for refracted rays. This causes 
  1184.      a blurred refraction, a.k.a. translucency.
  1185.  
  1186. `reflect_angle ANGLE'
  1187.      ANGLE is the maximum deviation for reflected rays. This causes a
  1188.      blurred reflection. a.k.a. Gloss.
  1189.  
  1190. `specular AMOUNT'
  1191. `ambient AMOUNT'
  1192. `diffuse AMOUNT'
  1193. `reflection AMOUNT'
  1194. `refraction AMOUNT'
  1195.      This scales the current specular, ambient, diffuse, reflection,
  1196.      refraction color by AMOUNT.
  1197.  
  1198.    For POV compatibility you can also specify
  1199.  
  1200. `color COLOR'
  1201.      This sets all colors (refraction, reflection, diffuse, ambient,
  1202.      specular) to COLOR, and the intensities to the default values
  1203.      used by POV.  (ambient 0.2, diffuse 0.6, etc.) From then on,
  1204.      these work POV compatible:
  1205.  
  1206.      You can also use
  1207.  
  1208. `specular AMOUNT'
  1209.      This sets the specular color to AMOUNT. The same can be done for
  1210.      the other colors:
  1211.  
  1212.           specular AMOUNT
  1213.           ambient AMOUNT
  1214.           diffuse AMOUNT
  1215.           reflection AMOUNT
  1216.           refraction AMOUNT
  1217.  
  1218.      Here is an example:
  1219.  
  1220.           #declare New_Brass = texture {
  1221.              color red 0.70  green 0.56  blue 0.37
  1222.              ambient 0.35
  1223.              diffuse 1.00
  1224.              brilliance 15
  1225.              specular 0.41
  1226.              roughness .2
  1227.           }
  1228.  
  1229. `imagemap { ... }'
  1230.      You can specify an imagemap. An imagemap wraps a bitmap picture
  1231.      onto a of the CSG, as far as they have been specifiedshape. This
  1232.      is the syntax:
  1233.  
  1234.           image_map { FILETYPE FILENAME IMAGE_MAP_MODIFIERS }
  1235.  
  1236.      This maps the file FILENAME onto the shape.  FILETYPE is the
  1237.      type of the image which is in FILENAME.  The following types for
  1238.      FILETYPE are supported:
  1239.  
  1240.     `tga'
  1241.           this specifies a targa 24 bit uncompressed type 2 (the same
  1242.           type Rayce outputs)
  1243.  
  1244.     `gif'
  1245.           This specifies that the image is a standard gif file.
  1246.  
  1247.      IMAGE_MAP_MODIFIER is one the following:
  1248.  
  1249.     `map_type TYPE'
  1250.           tells how the image should be wrapped around the object. 
  1251.           TYPE is one the following. The keywords are defined in
  1252.           `standard.inc'.
  1253.  
  1254.          `Plane_Map, 0'
  1255.                maps the image onto the XY plane
  1256.  
  1257.          `Sphere_Map, 1'
  1258.                maps the image onto sphere centered in (0, 0, 0)
  1259.  
  1260.          `Cylinder_Map = 2'
  1261.                maps the image onto cylinder along the Z axis.
  1262.  
  1263.          `Torus_Map, 5'
  1264.                maps the image onto a torus lying in the XZ plane,
  1265.                radius 1.0
  1266.  
  1267.     `uvswap'
  1268.           swaps the u and v parameters.  This is identical to
  1269.           rotating the bitmap 90 degrees.
  1270.  
  1271.           `uvrange'   UR, VR
  1272.  
  1273.           This scales the bitmap down by UR, VR, ie.
  1274.  
  1275.                image_map { "marijke.gif" map_type 3 uvrange 2 2 }
  1276.  
  1277.           will cause `marijke.gif' to be shrunk, until it fits 4
  1278.           times on a torus (two times along the big equator, two
  1279.           times along the small one)
  1280.  
  1281.     `uvoffset UO, VO'
  1282.           This translates the image by UO, VO.
  1283.  
  1284.     `once'
  1285.           If `once' is set, then the image can be seen only once.
  1286.  
  1287.     `interpolate INTERPOLTYPE'
  1288.           If you specify `interpolate' then interpolation is turned
  1289.           on. For POV compatibility, the interpolations should be
  1290.           followed by a integer INTERPOLTYPE. This is 0 for no
  1291.           interpolation (default), anything else for interpolation.
  1292.           The interpolation is bilinear, anything else is not
  1293.           supported
  1294.  
  1295.           These values are defined in `standard.inc'.
  1296.  
  1297.     `usenormal'
  1298.           This tells Rayce to use the surface normal instead of the
  1299.           object coordinate as an indexing point.
  1300.  
  1301.      The color of an imagemap at (x, y, z) in R^3 is computed as
  1302.      follows:
  1303.  
  1304.        1. First find u and v for the desired image.
  1305.  
  1306.        2. Swap u and v if uvswap is set.
  1307.  
  1308.        3. Scale: u = u*UR, v = v*VR
  1309.  
  1310.        4. Translate: u = u + UO, v = v + VO
  1311.  
  1312.        5. Determine color:
  1313.             1. if (once) and not (0.0 <= u, v <= 1.0) then return
  1314.                black.
  1315.  
  1316.             2. Get color at (u, v) in the image. If interpolation is 
  1317.                    turned on, then interpolate colors, else round the
  1318.                u, v      coordinates and use that coordinate
  1319.           Textures can be scaled, rotated and translated.
  1320.  
  1321. Texture Hierarchy
  1322. =================
  1323.  
  1324.    If an object doesn't have a texture, then Rayce gives it the
  1325. texture of of the CSG, as far as they have been specifiedits father.
  1326. Objects in Rayce have a hierarchy.
  1327.  
  1328.      #define C = union {
  1329.        A
  1330.        B
  1331.      }
  1332.      union { C }
  1333.  
  1334.    In this example, A and B are children of C, and C is a child of
  1335. the whole scene.  If Rayce registers a hit with primitive A, then it
  1336. first looks at A's texture.  If A doesn't have a texture, Rayce
  1337. checks C for a texture.  If C doesn't have a texture, then the Rayce
  1338. checks C's daddy.  C's daddy is the whole scene, and the whole scene
  1339. always has a texture, the default texture.
  1340.  
  1341.    You can change the texture by putting this in your input file:
  1342.  
  1343.      default = texture { TEXT }
  1344.  
  1345.    This will make TEXT the default texture. Take a look at `hier.r'
  1346. for an example of hierarchic textures.
  1347.  
  1348.  
  1349. Objects
  1350. ========
  1351.  
  1352.    In Rayce, objects are specified in this manner:
  1353.  
  1354.      SHAPENAME {
  1355.        SHAPE_BODY
  1356.      }
  1357.  
  1358.    or
  1359.  
  1360.      object {
  1361.        SHAPENAME {
  1362.          SHAPE_BODY
  1363.        }
  1364.      }
  1365.  
  1366.    SHAPENAME is one of the allowed shapes or a composite, SHAPE_BODY
  1367. the statements with info about the shape in it.  The difference
  1368. between these two ways of specifying is: you can use of the CSG, as
  1369. far as they have been specified`speed' and bounding shapes with the
  1370. latter.
  1371.  
  1372. Composites
  1373. ==========
  1374.  
  1375.    Rayce supports composites as well.  A composite collects a bunch of
  1376. shapes, and treats them as one.  This makes transforming and handling
  1377. a lot easier. A composite's syntax is the same as POV's:
  1378.  
  1379.      composite {
  1380.        [OBJECTs|COMPOSITEs]
  1381.      }
  1382.  
  1383.    Example:
  1384.  
  1385.      composite {
  1386.        box {  <-1, 1, 1>, <2, 3, 4> }
  1387.        object { heike }
  1388.        composite { Dikkerd }
  1389.      }
  1390.  
  1391. Object Modifiers
  1392. ================
  1393.  
  1394.    These transformations and on objects and composites are legal:
  1395.  
  1396. `texture { ... }'
  1397.      An object can have a texture.
  1398.  
  1399.      Example:
  1400.           object {
  1401.             sphere { <0, 0, 0>, 1 }
  1402.             texture { ambient color red 1.0 }
  1403.           }
  1404.  
  1405.      This is equivalent to
  1406.  
  1407.           sphere {
  1408.             <0, 0, 0>, 1
  1409.             texture { ambient color red 1.0 }
  1410.           }
  1411.  
  1412. `scale'
  1413. `rotate'
  1414. `translate'
  1415.      Any object can be transformed with `scale', `rotate' and
  1416.      `translate'.
  1417.  
  1418.      Translation, rotation and scaling will only affect a CSG or
  1419.      composite as far as it has been specified. All other modifiers
  1420.      apply to all contents in a composite or CSG. For more
  1421.      information, look at the definition of hexagon in `shapes.inc'
  1422.  
  1423. `inverse'
  1424.      Invert a shape.  This is only useful with  csg-able shapes.
  1425.  
  1426. `no_shadow'
  1427.      The object (and its children) are invisible to shadowrays. This
  1428.      object does not block light
  1429.  
  1430. `no_eye'
  1431.      The object (and its children) are invisible to eyerays. You can
  1432.      only see this object in reflections,refractions and shadows.
  1433.  
  1434. `no_refract'
  1435.      The object (and its children) are invisible to refraction rays.
  1436.      You cannot see this object through refractions
  1437.  
  1438. `no_reflect'
  1439.      The object (and its children) are invisible to reflection rays. 
  1440.      You cannot see this object through reflections
  1441.  
  1442. `speed <VELOCITY>'
  1443.      This says the composite will have a speed.  This is implemented
  1444.      by adding VELOCITY to the speed of each object in the object and
  1445.      its children. This acts on the shape as far as it has been
  1446.      specified.
  1447.  
  1448.      Note that only the bounding shapes of the noncomposite objects
  1449.      ("the leafs in the tree") are moved along. So be careful about
  1450.      your bounding shapes with moving objects. This is why I
  1451.      sometimes find this motion blur a pain the reet, because it
  1452.      makes all kinds of stuff extra complicated. For a nice and
  1453.      smooth picture, don't forget to turn the number of frames from
  1454.      the `options {}' block up.
  1455.  
  1456.      Note: this motion blur is only linear. Implementing rotational
  1457.      blur is very expensive, so I haven't implemented it.
  1458.  
  1459.      Example:
  1460.  
  1461.           object {
  1462.             sphere { <0, 0, 0>, 1 }
  1463.             speed <0, 0, 1>
  1464.           }
  1465.           
  1466.           composite {
  1467.             composite { fubar }
  1468.             object { fubular }
  1469.             bounded_by { box  { <-1, -1, -1>, <1, 1, 1> } }
  1470.             speed <0, 0, 1>
  1471.           }
  1472.  
  1473.      However, this is not allowed:
  1474.  
  1475.           sphere {
  1476.             <0, 0, 0>, 1
  1477.             speed <0, 0, 1>  // WRONG! syntax error
  1478.           }
  1479.  
  1480. `bounded_by { ... }'
  1481.      Manual bounding shape
  1482.           bounded_by { BOUNDINGSHAPES }
  1483.  
  1484.      This means that the composite is bounded by BOUNDINGSHAPES. A
  1485.      lot of shapes, especially compound shapes, take up a lot of
  1486.      time. To speed this up, you can specify bounding shapes: simple
  1487.      shapes that encompass a much more complex shape. This bounding
  1488.      shape should be closed; my suggestion is to use a sphere or a
  1489.      box, if possible.
  1490.  
  1491.      If an object has bounding shapes, then all rays are first tested
  1492.      against these shapes, in the order which they have been
  1493.      specified.  If this intersection test fails, or closer
  1494.      intersections have been found previously, then the object itself
  1495.      is not tested.
  1496.  
  1497.      If the optimisation automatic (eg. BSP), and the object is a
  1498.      normal element of the scene (not a part of CSG, extrusion, conic
  1499.      etc), then the bounding usually is ignored.  It is used to
  1500.      determine the extent of a shape.
  1501.  
  1502.      If Rayce complains about an infinite shape that cannot be
  1503.      autobounded, then there are two options:
  1504.  
  1505.      1. The shape is really infinite, then it is best to leave it
  1506.      that way. Example: if you replace an infinite groundplane, with
  1507.      a large disk, then the tracing will be slower.
  1508.  
  1509.      2. The shape is finite, and not big compared to the rest of the
  1510.      scene. Then you should use `bounded_by'.
  1511.  
  1512.      Example:
  1513.  
  1514.           object {
  1515.             algebraic { $ x^4 + y^4 + z^4 - 1 $ }
  1516.             bounded_by { box  { <-1>, <1> } }
  1517.           }
  1518.           
  1519.           composite {
  1520.             composite { fubar }
  1521.             object { fubular }
  1522.             bounded_by { box  { <-1>, <1> } }
  1523.           }
  1524.  
  1525.      This is not allowed:
  1526.  
  1527.           algebraic {
  1528.             $ x^4 + y^4 + z^4 - 1 $
  1529.             bounded_by { box  { <-1>, <1> } }
  1530.               // WRONG! syntax error
  1531.           }
  1532.  
  1533. `clipped_by { ... }'
  1534.      This clips the object with a shape. Syntax:
  1535.  
  1536.           clipped_by { CLIPPINGSHAPES }
  1537.  
  1538.      The object is clipped by CLIPPINGSHAPES. A clipping shape is the
  1539.      Procrustes in raytracing: Rayce cuts off all parts of an object
  1540.      that stick out of the CLIPPINGSHAPES. If you want to use a shape
  1541.      as bounding shape and clipping shape simultaneously, then you
  1542.      should use
  1543.  
  1544.           clipped_by { bounded_by }
  1545.  
  1546.      Example:
  1547.  
  1548.           #include "shapes.inc"
  1549.           object {
  1550.             object { Cylinder_X }    // now it's infinite
  1551.             clipped_by {
  1552.               box <-1, -1, -1>, <1, 1, 1>
  1553.               sphere { <0, 0, 0>, 100 }
  1554.             }
  1555.             // now it's a tube, and you can look inside.
  1556.           }
  1557.           
  1558.           composite {
  1559.             composite { fubar }
  1560.             object { fubular }
  1561.             bounded_by { box { <-1, -1, -1>, <1, 1, 1> } }
  1562.             clipped_by { bounded_by }
  1563.           }
  1564.  
  1565.  
  1566. Special Commands
  1567. =================
  1568.  
  1569.    If your scene file says:
  1570.  
  1571.      #include "foobar.inc"
  1572.  
  1573.    then the parser will act as if the contents of `foobar.inc' are
  1574. inserted at this point.  The `#' is optional: since the POV team
  1575. hasn't made it mandatory, all `#'s in the input are treated is
  1576. whitespace: they are ignored.  If a file is opened, `[' is shown, and
  1577. an "operator pacification indicator" (`.')  is printed every 25 lines
  1578. read, after a file has been read `]' is printed.
  1579.  
  1580.    If you use objects, cameras, etc. a lot, you should `#define'
  1581. them, and put them in an include for the greater benefit of the human
  1582. race:
  1583.  
  1584.      #define foo = sphere { <0, 0, 9>, 1 }
  1585.  
  1586.    means you can use
  1587.  
  1588.      object { foo }
  1589.  
  1590.    instead of
  1591.  
  1592.      object { sphere { <0, 0, 9>, 1 }
  1593.  
  1594.    These can be defined:
  1595.  
  1596.      any shape:      #define foo = sphere { ... }
  1597.      camera:         #define foo = camera { ... }
  1598.      color:          #define foo = color  ...
  1599.      texture:        #define foobar = texture { ... }
  1600.      vector expressions:
  1601.                      #define fub = <0, 1, 0>^<1, 0, 0>
  1602.      floating point expressions:
  1603.                      #define blab = 1.34/2
  1604.      image_maps:     #define auk = image_map { ... }
  1605.  
  1606.    The following obfuscated construct should work correctly:
  1607.  
  1608.      #define foo = object { sphere { bla } texture { foo } }
  1609.  
  1610.    If you attempt to save memory, then don't use defines: they only
  1611. cost more memory during the parsing stage.
  1612.  
  1613.    You can use the keyword `declare' instead of `define'. The effect
  1614. on Rayce is the same. Rayce has no CPP preprocessor, so the `define'
  1615. stuff doesn't have to be on one line.
  1616.  
  1617.    I think the latter is better. As a bonus, a `#define' at the start
  1618. of a line is recognized by certain utilities for the C programming
  1619. language (eg. [ce]tags)
  1620.  
  1621.  
  1622. POV compatibility
  1623. ******************
  1624.  
  1625.    Rayce started out as an experiment, and the POV language seemed
  1626. simple to implement. That's why the syntax looks like POV so much. 
  1627. When I designed it, POV 2 wasn't out yet, so it's not completely
  1628. compatible. The format is loosely based on POV1.
  1629.  
  1630.    I call it semi-POV compatible. These are incompatibilities:
  1631.  
  1632.    [note. I don't use POV that often anymore, there are probably some
  1633. things which aren't correct]
  1634.  
  1635.    What Rayce misses:
  1636.  
  1637.    * solid texturing
  1638.  
  1639.    * bumps
  1640.  
  1641.    * serious antialiasing.
  1642.  
  1643.    * `phong' syntax: use the `microfacet' directive
  1644.  
  1645.    * support for these primitives
  1646.  
  1647.         - Heightfield
  1648.  
  1649.         - Bezierpatch
  1650.  
  1651.         - Cone
  1652.  
  1653.         - Poly/Cubic/Quartic (the `algebraic {}' allows you to do the
  1654.           same, but with a better syntax)
  1655.  
  1656.         - Blob
  1657.  
  1658.    * difference (use an intersection and `inverse')
  1659.  
  1660.    * `filter' color components
  1661.  
  1662.    Caveats with porting scenes
  1663.    * don't use pigment/normal/finish blocks; use POV1 style texture
  1664.      blocks.
  1665.  
  1666.    * The color of a texture should be the first with POV style
  1667.      textures.
  1668.  
  1669.    * camera model is slightly different. Use `aspect' for aspect
  1670.      ratios
  1671.  
  1672.    * in POV2, `unions' are called `merges', and `composites'
  1673.      `unions'. So a POV2 merge corresponds with a Rayce union, and a
  1674.      POV2 union corresponds with a Rayce composite.
  1675.  
  1676.    What Rayce has (and POV doesn't)
  1677.    * more flexible texturing for even colors.
  1678.  
  1679.    * these primitives:
  1680.  
  1681.         - extrusions.
  1682.  
  1683.         - polygons.
  1684.  
  1685.         - superquadrics.
  1686.  
  1687.    * ability to manipulate imagemaps with uvswap, uvrange, etc.
  1688.  
  1689.    * motion blur.
  1690.  
  1691.    * gloss, translucency.
  1692.  
  1693.    * light that attenuates with the distance.
  1694.  
  1695.    * microfacet models
  1696.  
  1697.    * fog sources.
  1698.  
  1699.    Syntax incompatibilities
  1700.  
  1701.    Usually, Rayce's syntax is more relaxed. These are extras:
  1702.  
  1703.    * <a> as vector exp.
  1704.  
  1705.    * this is legal:
  1706.      composite {
  1707.        object { bla }
  1708.        rotate 30*y
  1709.        object { bla }
  1710.        rotate 30*y
  1711.      }
  1712.  
  1713.  
  1714. Technical Stuff
  1715. ****************
  1716.  
  1717.    This is supposed to be a more detailed doc file about the
  1718. technicalities of Rayce. It is under construction (as is everything
  1719. in Rayce :-)
  1720.  
  1721.  
  1722. About The Sources
  1723. ==================
  1724.  
  1725.    I've tried to continue the Ray-tradition: I've commented the
  1726. sources to my best ability, and I hope I didn't obfuscate the sources
  1727. too much. All sources have been formatted with GNU indent (see the
  1728. file `cformat').  I have always use 132 x 43 screen resolution, so
  1729. some of the source will not be very readable in 80x25....  Get
  1730. yourself a serious VGA card :)
  1731.  
  1732.  
  1733. Compiling
  1734. ==========
  1735.  
  1736.    Remember these if you attempt at compiling Rayce:
  1737.  
  1738.   1. Get a ANSI compliant compiler. I don't know wether it will
  1739.      compile on non ANSI compilers, but it will undoubtedly give
  1740.      problems.
  1741.  
  1742.   2. I've tried to keep all device/compiler dependent stuff in a
  1743.      separate file.  A generic version of this file is in `dvi.c'; it
  1744.      provides only the stubs for the functions.
  1745.  
  1746.      If you want to port Rayce, then make a new Makefile and a
  1747.      replacement for the systemfile. look at `ibmtcc.c' or `linux.c'
  1748.      to see which functions it should have, and what they should do. 
  1749.      It would be nice if you sent me a copy of the source, and the
  1750.      makefile.  If you are very lazy, you can use `dvi.c'. The
  1751.      `linux.c' file only uses ANSI standard routines, so you should
  1752.      be able to use `linux.c' for most OSes which have signals.
  1753.  
  1754.      These files are provided:
  1755.  
  1756.     `makefile.tcc, ibmtcc.c'
  1757.           for Turbo/Borland-C++
  1758.  
  1759.     `Makefile, linux.c'
  1760.           for Linux
  1761.  
  1762.     `makefile.dj, djgcc.c'
  1763.           for DJGCC (the port was done by Shawn McHorse
  1764.           <Zaphod@fcircus.sat.tx.us>)
  1765.  
  1766.   3. Although I tried to avoid system dependent code outside the
  1767.      systemfiles, there are some exceptions:
  1768.  
  1769.         * the naming of MSDOS is somewhat braindead, so in `token.c',
  1770.           there is a bit of conditional compilation for including
  1771.           `rayparse.h' instead of `rayparse.tab.h' under MSDOS.
  1772.  
  1773.         * in `rayparse.y' it says
  1774.  
  1775.                #ifdef __TURBOC__
  1776.                #define alloca allocaa
  1777.                #endif
  1778.  
  1779.   4. Make sure that Rayce has enough memory.  In MSDOS this means: a
  1780.      reasonable stack size, and a large memory model.  Turbo C's 4k
  1781.      stack is definitely too small, but 16k suffices.
  1782.  
  1783.   5. Get some variant of Yacc. I use Bison, but any other Yacc will
  1784.      probably also work. If you use Bison, and your compiler has no
  1785.      `alloca()' function, you'd better define alloca to be allocaa in
  1786.      `rayparse.y'.  Turbo C doesn't have `alloca()', so I made a fix:
  1787.      memory obtained `allocaa()' is freed after `yyparse()' has
  1788.      exited.
  1789.  
  1790.   6. If you use Linux, it is very easy, just type
  1791.           make sRayce
  1792.      for a speedy version of Rayce.
  1793.  
  1794.  
  1795. Source Description 1
  1796. =====================
  1797.  
  1798. `ray.h'
  1799.      typedefs and important `#defines' for Rayce. The most important
  1800.      ones:
  1801.  
  1802.      `object': This is the main structure. It holds shape data,
  1803.      textures, boundingshapes. It has a `next' field, so it can
  1804.      easily be linked into a linked list.
  1805.  
  1806.      `struct ray': this is (of course) the ray. It is a line,
  1807.      starting at ray.pos, headed for ray.dir.
  1808.  
  1809.      A few important notes:
  1810.  
  1811.      If you want to study the sources of Rayce, you'd better start
  1812.      reading here.
  1813.  
  1814.      A function or variabele that is local to a file is called
  1815.      `PRIVATE'.  A function or variabele that can be accessed from
  1816.      the whole program is called `PUBLIC'.
  1817.  
  1818.      The vector as well as the color are structs, not arrays, so they
  1819.      must be passed by pointer, if function wants to change them. But
  1820.      the advantage is, they can be returned from a function.
  1821.  
  1822. `object.h'
  1823.      a collection of `struct xxxx_data's; structures which hold info
  1824.      on shapes, textures, etc. eg. sphere_data contains a radius and
  1825.      a center.
  1826.  
  1827. `proto.h'
  1828.      This file contains function prototypes, to avoid the compilers'
  1829.      weird ideas about default prototyping. Modifying its contents
  1830.      usually is harmless, so the makefile just ignores it.
  1831.  
  1832. `extern.h'
  1833.      Global variables.  Because this header is included more than
  1834.      once, all variables have been declared as `EXTERN', which is
  1835.      defined as extern.  In `initialize.c', `EXTERN' is undef'ed, and
  1836.      included again.  So storage for global variables is only
  1837.      allocated once. Important ones: commandline options, statistics.
  1838.  
  1839. `parse.h'
  1840.      for communication between `rayparse.y' and `token.y'
  1841.  
  1842. `rayparse.y'
  1843.      The Bison sources for the parser.
  1844.  
  1845. `rayparse.c'
  1846. `rayparse.tab.c'
  1847. `rayparse*.h'
  1848.      Bison generates these files from `rayparse.y'. I don't distribute
  1849.      them, because you can generate them yourself in a few seconds.
  1850.  
  1851. `vector.h'
  1852.      Vector macros. They usually have this form
  1853.  
  1854.           vectormacro(output, input1, input2).
  1855.  
  1856.      Take care: usually a construction like `vsub(a,a,b)' (equivalent
  1857.      with a:=a-b) works fine, but you can't do this with vcross.
  1858.  
  1859.      [Yes. I know, GCC allows inline functions, but I don't want to go
  1860.      through all the hassle and compiler dependent code.]
  1861.  
  1862. `macro.h'
  1863.      Other useful macros.
  1864.  
  1865. `ibuf.h'
  1866.      Macros for the intersection queues.
  1867.  
  1868. `token.c'
  1869.      Tokenizer, handles includes and allocation of declares.  The most
  1870.      important function is `yylex()'. The rest are little bits. If
  1871.      you add keywords to the syntax, then add it into the big table
  1872.      in this file. The keywords should in alphabetical order.
  1873.  
  1874. `messages.c'
  1875.      Error messages, debugging logs and memory allocation routines:
  1876.      `errormsg()', `warning()', `dprintf()'.
  1877.  
  1878. `rayparse.y'
  1879.      This is the Yacc/Bison source file.  Run `bison -d rayparse.y' to
  1880.      get `rayparse_tab.c' and `rayparse.tab.h'.  On MSDOS systems
  1881.      these names will be truncated to `rayparse.c' and `rayparse.h'.
  1882.  
  1883. `raymath.c'
  1884.      Linear algebra and simple mathematics:  vector math, matrix
  1885.      math, random generators.
  1886.  
  1887. `ibmtcc.c'
  1888. `linux.c'
  1889. `djgcc.c'
  1890. `dvi.c'
  1891.      Compiler/system specific parts: timing, handlers, abort checks.
  1892.      Although they are still pretty portable, you could substitute
  1893.      `dvi.c' for this one, if your compiler has complaints. If you
  1894.      would like to code display routines, this is where you should
  1895.      add them. I myself don't like device dependent stuff in a
  1896.      program which is supposed to be highly portable.
  1897.  
  1898. `main.c'
  1899.      The file containing `main()' it scans the command line, prints
  1900.      notices, calls the parser and starts the tracing process.
  1901.  
  1902. `trace.c'
  1903.      Sets up the tracer, walks all the pixels, and calls `trace()'
  1904.      for each pixel.  Ray sampling is done here, and stat printing is
  1905.      done here too.
  1906.  
  1907. `intersect.c'
  1908.      Small file with main intersection routine:  it does the
  1909.      intersection with the whole scene, and then fills in the details
  1910.      such as intersection points, normals. The shadow test is a
  1911.      separate routine, because it has to deal with shadow caching,
  1912.      early exit for blocking objects, etc.
  1913.  
  1914. `initialize.c'
  1915.      Initialization and stuff which I can't put somewhere else. Global
  1916.      storage allocation.
  1917.  
  1918. `bg.c'
  1919.      Compute a background color, maybe too small to be a separate
  1920.      file, and too fancy.  Who needs gradient background? Also adds
  1921.      fog influence to traced rays.
  1922.  
  1923. `poly.c'
  1924.      Manipulations of polynomials in one variable: addition,
  1925.      multiplication, negation, power, etc. Used by `solve.c' and
  1926.      `algebraic.c'. NB. If you want to do q(x) := q(x) - t(x), then
  1927.      `poly_sub(q, q, t)' is OK. But `poly_multiply(q,q,t)' for q :=
  1928.      q*t is not!
  1929.  
  1930. `solve.c'
  1931.      Solve polynomial equations of arbitrary order.  NB.  It's main
  1932.      entry point is the function `solve_rt_poly()' which finds
  1933.      positive roots of a polynomial, and puts them sorted in an
  1934.      array. If possible, solve_rt_poly uses fast root finders for
  1935.      2nd, 3rd and 4th order equations. If you want to have accurate
  1936.      results, use `sturm_solve_rt_poly().'
  1937.  
  1938.      The code was done by the collective Eric H. Echidna (who have
  1939.      written a nice raytracer called Art). I also added bits of
  1940.      myself and Graphics Gems. Ok. It's not my code, but I'll try
  1941.      explain what this is all about.
  1942.  
  1943.      This module solves polynomial equations, or equations that look
  1944.      like:
  1945.  
  1946.           a_n x^n + a_(n-1) x^(n-1) + ... + a_0 = 0
  1947.  
  1948.      n is called the order of the equation, is denoted by `ord' in the
  1949.      code.  Note that any solution p(s) = 0 to the equation also is a
  1950.      root of the polynomial: if p(s) = 0 is a solution, then the
  1951.      polynomial can be written as p(x) = q(x) (x-s), with degree(q) =
  1952.      degree(p) - 1.
  1953.  
  1954.      In this application we are only interested in so called "simple
  1955.      roots", i.e. if s is a root ( p(s) = 0 and p(x) = q(x) (x-s) ),
  1956.      then q(s) != 0.
  1957.  
  1958.      This has numerical reasons: take (for example) p(x) := (x-1)^2. p
  1959.      clearly has a double root: p(1) = 0. But if we would start
  1960.      calculating, intermediary results would look like it came from
  1961.      p(x) = (x-1)^2 + 1e-20, which has no solutions. So double roots
  1962.      can be hard to detect.
  1963.  
  1964.      If n <= 4, then an "exact" solution is possible: an out of the
  1965.      box solution is possible. However, it is numerically unstable. 
  1966.      It has been proved that for n > 4, no general exact solution
  1967.      method exists.
  1968.  
  1969.      If n > 4, we have to resort to numerical techniques. The first
  1970.      attempt used was `equation_solver()', which has been `#undef''d
  1971.      ^H^H^Hdeleted, since it is 3x slower for quartics than sturm
  1972.      sequences.
  1973.  
  1974.      It worked like this: let us assume p(t) = 0 is the equation we
  1975.      want to solve.
  1976.  
  1977.      1. if degree <= 3, then solve exactly
  1978.  
  1979.      2. if degree > 3, then take derivative, (recursive) find it's
  1980.      roots with `equation_solver().'
  1981.  
  1982.      These roots of the derivative are the extrema of p(t). We then
  1983.      calculate these extrema. Additionally, p(0) and p(LARGE) are
  1984.      boundary extrema. Any zero of p would lie between two extrema
  1985.      with different sign, and (vice versa) Between two extrema with
  1986.      different signs there always is a zero. We can find such a zero
  1987.      with binary chop.
  1988.  
  1989.      The sturm sequence uses also this property: A sturm sequence can
  1990.      be used to detect sign changes of p(x) for x in the interval
  1991.      [a,b).
  1992.  
  1993.      A sturm sequence is a sequence of polynomials,
  1994.  
  1995.           p_0, p_1, ... p_m
  1996.  
  1997.      because it's a sturm sequence, these polynomials have special
  1998.      properties. From these properties, it can be deduced that the
  1999.      number of sign changes in p_0(x) in the interval [a,b) is w(a) -
  2000.      w(b). Here, w(c) means the number of sign changes in
  2001.  
  2002.           p_0(c), p_1(c), ... p_m(c)
  2003.  
  2004.      A sturm sequence can easily be constructed using polynome
  2005.      division. For details: see J. Stoer, R. Bulirsch "Introduction
  2006.      to Numerical Analysis".
  2007.  
  2008.      The number of sign changes between a and b equals the number of
  2009.      roots between a and b. So we could use binary chop too: after
  2010.      we've divided [a,b) into [a,c) and [c,b) we can check the number
  2011.      of signchanges in [a,c) and [c,b), until the number of
  2012.      signchanges is one, and then we're sure that one root is sitting
  2013.      in the interval.
  2014.  
  2015.      3d degree equations are solved using using Cardano's method. It
  2016.      is numerically unstable with double roots.
  2017.  
  2018.      How does it work?
  2019.  
  2020.      Suppose we have an cubic equation
  2021.  
  2022.           ax^3+ bx^2 + cx + d = 0
  2023.  
  2024.      After division by a, we get
  2025.  
  2026.           x^3 + b' x^2 + c' x + d' = 0
  2027.  
  2028.      After substitution x = y -b'/3. We get
  2029.  
  2030.           y^3 = 3py - 2q        (1)
  2031.  
  2032.      For certain p and q. This is the big Cardano trick: we could
  2033.      solve the equation if we would find u and v such that
  2034.  
  2035.           u^3 + v^3  = -2q and uv = p            (2)
  2036.  
  2037.      since (u+v)^3 = u^3 + 3u^2v + 3v^2u + v^3. = 3(u+v) uv + u^3 +
  2038.      v^3
  2039.  
  2040.      and y would be y = u+v. (2) turns out to be a quadratic
  2041.      equation, and it gives us one solution for (1).  If (2) has no
  2042.      solution (discriminant 4q^2 - 4p^3 < 0), an other method has to
  2043.      used. We can safely assume that y = 2sqrt(p) cos(theta), and (1)
  2044.      turns out to be
  2045.  
  2046.           cos(3 theta) = -q/(p*(sqrt(p)))
  2047.  
  2048.      This gives us 3 solutions.
  2049.  
  2050. `grids.c'
  2051.      Keeps trak of distribution raytracing grids and sampling them.
  2052.  
  2053. `bsp.c'
  2054.      Binary space partitioning efficiency scheme. Hacked from
  2055.      Graphics Gems III.
  2056.  
  2057. `optimise.c'
  2058.      Manipulate lists of objects to be optimised.
  2059.  
  2060. `shade.c'
  2061.      Compute the color of a ray using textures, light_sources etc. 
  2062.      This used to be the best preserved part of the original Ray
  2063.      raytracer.  But Shawn McHorse revised it completely to allow
  2064.      TIR, microfacets, etc.  This file recursively calls `trace()'
  2065.  
  2066. `imagemap.c'
  2067.      Stuff for imagemaps: reading TGAs, mapping planes onto various
  2068.      shapes, and (of course) standard methods.
  2069.  
  2070. `gif.c'
  2071.      The code for reading gif files. This file was snatched from a
  2072.      Linux Gif viewer. It was created by Harm Hanemaayer
  2073.      <hhanemaa@cs.ruu.nl>, and it was based on posting in
  2074.      rec.games.programmer in the spring of 1993.
  2075.  
  2076. `ibuf.c'
  2077.      This implements a smart depth queue for keeping intersection
  2078.      information.
  2079.  
  2080. `texture.c'
  2081.      Texture manipulations.
  2082.  
  2083. `camera.c'
  2084.      Routines for manipulating cameras. Not very useful, tho.
  2085.  
  2086.  
  2087. Source Description 2
  2088. =====================
  2089.  
  2090.    The files below are files with object and shape standard_routines.
  2091. The file xxx.c usually contains:
  2092.  
  2093. `free_xxx()'
  2094.      free a xxx structure (and it's components)
  2095.  
  2096. `get_new_xxx_object()'
  2097.      alloc and init a xxx structure (don't alloc components)
  2098.  
  2099. `rotate_xxx(),'
  2100. `scale_xxx(),'
  2101. `translate_xxx()'
  2102.      transform a xxx structure
  2103.  
  2104. `all_xxx_intersections()'
  2105.      intersect a ray with a xxx structure, and put some or all
  2106.      intersections into a queue. Give info, wether ray.pos is inside
  2107.      shape.
  2108.  
  2109. `xxx_normal'
  2110.      the normal to a xxx shape.
  2111.  
  2112. `copy_xxx'
  2113.      copy a xxx structure (and it's components)
  2114.  
  2115. `inside_xxx'
  2116.      is a point inside an xxx shape?
  2117.  
  2118. `precompute_xxx'
  2119.      Compute constants for a certain object before tracing has
  2120.      started, but after the parsing.  Objects should be automatically
  2121.      bounded here.
  2122.  
  2123. `print_xxx'
  2124.      Print useful information about a xxx, very useful for debugging.
  2125.  
  2126.    These routines are stored in a method structure. With each
  2127. instance of a XXX, a pointer to XXX's methods is stored. So a sphere
  2128. object carries a pointer to its own special routines. These routines
  2129. can only be accessed using this method structure, since the routines
  2130. are `PRIVATE'.
  2131.  
  2132.    If you want to add your own primitive this is what you have to do:
  2133.  
  2134.   1. Make your own routines, and put them into a seperate file.  Take
  2135.      a look at the implementation of some simple shapes, such as
  2136.      polygon, or sphere. Use `stub.c' as a starting point.
  2137.  
  2138.   2. Add the new datastructures to `objects.h'
  2139.  
  2140.   3. define the syntax of your primitive, and please make it POV like.
  2141.      Add it to the shape_block. Add the keywords to `token.c' and
  2142.      `rayparse.y'.  Here are fragments to get you started:
  2143.  
  2144.           /* rayparse.y */
  2145.           
  2146.           /* token declarations */
  2147.           %type <objectval> XXXX_block    XXXX_body
  2148.           %token  XXXX_T
  2149.           %token <dectabptr> XXXX_IDENTIFIER
  2150.           
  2151.           
  2152.           /* syntax */
  2153.           XXXX_body: /* how to specify a XXXX */
  2154.               {
  2155.                 $$ = get_new_XXXX_object();
  2156.           
  2157.                 /* no precomputation here! */
  2158.               }
  2159.               | XXXX_body TRANSLATE vexp        { translate_object($$, $3); }
  2160.               | XXXX_body ROTATE vexp           {
  2161.                 matrix rm; make_rotation_matrix(rm,$3);rotate_object($$, rm);
  2162.               }
  2163.               | XXXX_body scale_stuff           { scale_object($$, $2); }
  2164.               | XXXX_body INVERSE               { $$->inverted = !$$->inverted; }
  2165.               | XXXX_body texture_block { $$->text = $2; }
  2166.               ;
  2167.           
  2168.           
  2169.           XXXX_block: XXXX_T '{' XXXX_body '}'  { $$ = $3; }
  2170.               ;
  2171.           
  2172.           
  2173.           shape_block:
  2174.           /*  ...   */
  2175.               | XXXX_block {$$ = $1 }
  2176.               ;
  2177.           
  2178.           
  2179.           /* token.c, keyword table (it's in alfabetical order) */
  2180.           {
  2181.                 :
  2182.                 :
  2183.               XXXX_T, "xxxx",     /* remember! sorted by alphabet */
  2184.                 :
  2185.                 :
  2186.           }
  2187.  
  2188.   4. You're finished! Presto! Test your primitive, and then mail the
  2189.      modified sources to me.
  2190.  
  2191. `box.c'
  2192.      Rectangular boxes. It also contains a public procedure
  2193.      `intersect_rawbox()', for intersecting a ray with an axis aligned
  2194.      box.  The algorithm is standard. This function is public,
  2195.      because it could be used for bounding shapes.
  2196.  
  2197. `composite.c'
  2198.      A composite is just a bunch of objects which can be treated as
  2199.      one. This makes collective scaling, rotating etc. very easy.
  2200.      Note that in Rayce, this is something different than a CSG union.
  2201.  
  2202. `lights.c'
  2203.      A very simple file with routines for the lights objects. Code to
  2204.      create a linked list of all the lights in the scene is also
  2205.      here.  This list is needed for efficiently finding the lights in
  2206.      the scene, in `shade.c' and `bg.c'.
  2207.  
  2208. `plane.c'
  2209.      Manipulate infinite planes.
  2210.  
  2211. `object.c'
  2212.      A uniform interface to the various shapes and object types in
  2213.      Rayce.
  2214.  
  2215. `quadric.c'
  2216.      Code for arbitrary quadrics.
  2217.  
  2218. `sphere.c'
  2219.      Spheres, the most basic thing in raytracing (along with checkered
  2220.      floors ;-) (gee, I must do checkers, some time)
  2221.  
  2222. `superq.c'
  2223.      Superquadrics. It should work now.
  2224.  
  2225. `torus.c'
  2226.      Toruses. Simple toruses. They can be calculated quite
  2227.      efficiently, because it is possible to have very tight bounds
  2228.      around them. Uses `solve.c' to do the equation solving.
  2229.  
  2230. `csg.c'
  2231.      Code for CSG unions and intersections. Since the code for both
  2232.      overlaps, they are contained in one file. Mathematically
  2233.      speaking, they are even equivalent!
  2234.  
  2235. `algebraic.c'
  2236.      Arbitrary algebraic surfaces. The general idea is: store the
  2237.      polynomial in terms of operations (similar to hoc level 4, in
  2238.      The Unix Programming Environment, by Kernighan & Pike).  This is
  2239.      done in `rayparse.y'. Then use a simple "virtual" stack machine
  2240.      to calculate the resulting polynomial in the ray parameter t.
  2241.      The normal is calculated in a similar fashion: the input is
  2242.      vector Loc, and of all operands (which are polynomials p_j), the
  2243.      polynomial p_j(x1,x2,x3) in Loc is evaluated, and the polynomial
  2244.      dp_j/dx_i (i = 1...3) is evaluated in Loc. We then get the
  2245.      gradient of the polynomial Phi(x,y,z) which determines the
  2246.      surface normal
  2247.  
  2248. `polygon.c'
  2249.      Flat polygons. Convex polygons are implemented by clipping a
  2250.      plane with the edges of a polygon. The polygon is projected onto
  2251.      a coordinate plane before it is clipped. Concave: ripped from
  2252.      MTV, it uses the Jordan Curve theorem
  2253.  
  2254. `extrusion.c'
  2255.      Translational extrusions of 2D curves. This takes the
  2256.      intersection of a shape with the XY plane, and then "thickens"
  2257.      it along the Z axis. I have concocted the algorithm myself, but
  2258.      it turns out Kajiya and van Wijk already did it before me.
  2259.  
  2260. `triangle.c'
  2261.      Smooth and non-smooth triangles. It works by decomposing an
  2262.      intersection point into barycentric coordinates.
  2263.  
  2264. `conic.c'
  2265.      conic extrusion.
  2266.  
  2267.  
  2268. Me
  2269. ***
  2270.  
  2271.    About the author: I am a student, as of today (Januari 15 '94) I am
  2272. 19, and I study applied mathematics at the Eindhoven University of
  2273. Technology, in the Netherlands.  I am in my second year, and I spend
  2274. too little time on my study. Oh well. Raytracing is my hobby. If I am
  2275. not spending my time behind my computer I also play the French horn
  2276. and the piano.
  2277.  
  2278.    The following have been helpful in developing Rayce:
  2279.  
  2280.    * the sources to MTV, POV, Rayshade, Vort, Ray, Graphics Gems
  2281.  
  2282.    * The fractal.028 and PCGNet Raytracing echo community for making
  2283.      me happy with mail.
  2284.  
  2285.    * "Tutorial: Computer Graphics: Image Synthesis", Kenneth I Joy,
  2286.      a.o.  ISBN 0-8186-8854-4.
  2287.  
  2288.    * "Three Dimensional Computer Graphics", Alan Watt, Addison Wesley.
  2289.  
  2290.    * "Advanced Rendering and Animation Techniques", Alan Watt & Mark
  2291.      Watt, Addison Wesley.
  2292.  
  2293.    * Linus Torvaldsen, for creating Linux, an excellent free Unix
  2294.      operating system for the 386/486 PCs. Try it! It's the best
  2295.      thing since sliced bread (The next best thing after linux being
  2296.      Rayce, of course! :)
  2297.  
  2298.    * A lot more books, after a while, I didn't check any titles. If
  2299.      you want a recommendation: here are some:
  2300.         - Glassners book "An intro to RayTracing." The best book on
  2301.           raytracer-writing available
  2302.  
  2303.         - "Advances in Computer Graphics IV. "  These proceedings
  2304.           contain a good introductory paper on Ray tracing
  2305.  
  2306.    If you want something implemented, or if you want to hack something
  2307. yourself: please contact me, so other people can benefit from it too.
  2308. I also appreciate any comment on the Rayce, its sources and its docs.
  2309.  
  2310.    I hope that all mathematics were understandable, and that my
  2311. English doesn't show that I normally speak Dutch.
  2312.  
  2313.    Send your compliments, beer, bugreports and daughters to:
  2314.  
  2315.      Han-Wen Nienhuys
  2316.      Dommelseweg 1A
  2317.      5581 VA Waalre
  2318.      The Netherlands
  2319.  
  2320.    But email is preferred for the compliments and bugreports, of
  2321. course:
  2322.  
  2323.      hanwen@stack.urc.tue.nl
  2324.  
  2325.    If you can't find me there, then try my dad's account:
  2326.  
  2327.      wsadjw@urc.tue.nl
  2328.  
  2329.    If you are connected to fidonet, then you can reach me at
  2330.  
  2331.      2:284/102.27
  2332.  
  2333.    My PCGNet address is
  2334.  
  2335.      9:580/203.56
  2336.  
  2337.    You could also try post a message to GRAPHICS, the international
  2338. fidonet graphics echo. I regularly read the PCGNet raytracing echo,
  2339. and I am subscribed to the DKB-L mailing list.
  2340.  
  2341.    I will try to have the latest version of Rayce available at
  2342.  
  2343.      Bennekom BBS: fido node 2:283/203, PCGNet 9:580/203, tel.
  2344.      31-8389-15331. (freqqing times: 07.30 to 01.00, local time in
  2345.      Holland)  Bennekom BBS supports V32bis and V42bis.
  2346.      
  2347.      `wuarchive.wustl.edu:/graphics/graphics/ray/rayce'
  2348.  
  2349.  
  2350. Bugs
  2351. *****
  2352.  
  2353.    Yes.
  2354.  
  2355.    But seriously :), I am not a big tester myself. It is almost
  2356. certain that Rayce contains bugs; while developing a complicated
  2357. scene, you will probably encounter some.
  2358.  
  2359.    Maybe you will see
  2360.  
  2361.      assert failed file XXX.c, line XXX
  2362.  
  2363.    These are messages you shouldn't ever get to see, they show that
  2364. some internal consistency check failed. If you see this, then please
  2365. mail a bugreport to me.
  2366.  
  2367.    (On the other hand, if it came from `csg.c', you are probably not
  2368. using correct shapes. Make sure extrusions and cylinders are closed,
  2369. and use the `closed' keyword correctly on algebraics)
  2370.  
  2371.    If Rayce crashes, that's obviously subversive behaviour of Rayce.
  2372. Especially if the crashes are violent, you should report them.
  2373.  
  2374.    If you find very subtle bugs, I am also very interested. On the
  2375. average, it takes me two new versions before I discover those kind of
  2376. errors. I should do more testing.  Yes. I know. But coding is so much
  2377. more fun.
  2378.  
  2379.    Known bugs:
  2380.  
  2381.    * The conic extrusion is BETA. It doesn't work.
  2382.  
  2383.    * Some algebraics don't render correctly. This is probably caused
  2384.      by the root finder, which was copied from Vort almost verbatim.
  2385.  
  2386.    * Some algebraics have singular points, where there is no normal
  2387.      to the surface (the normal is <0,0,0> ). When this happens,
  2388.  
  2389.           NNV # XXX.c
  2390.  
  2391.      is printed, with line number and the file where it happened.
  2392.  
  2393.    * Some algebraics aren't fun to calculate precisely. If you use
  2394.      sturm sequences, you will sometimes see solve.c scream if you use
  2395.      the runtime warnings turned on (`-d4'). This happens because the
  2396.      sturm code first tries a fast way of calculating a root.
  2397.  
  2398.    Check out the files `ONEWS' and `ChangeLog' on resolved bugs, and
  2399. `TODO' on stuff that needs to be done.
  2400.  
  2401.  
  2402. Distribution Raytracing
  2403. ************************
  2404.  
  2405.    Detailed information on what distribution ray tracing is can be
  2406. found in the paper by Cook, Porter and Carpenter: "Distributed Ray
  2407. Tracing", ACM Computer Graphics, Vol.18, Num.3, July 1984.
  2408.  
  2409.    [Note: what follows is a interpretation of what I think distributed
  2410. raytracing is, so no garantuee is made that it is correct. ]
  2411.  
  2412.    Distribution raytracing is a process used to model "fuzzy"
  2413. phenomena, such as soft shadows, focal blur, motion blur, fuzzy
  2414. reflections and fuzzy refractions. In raytracing, refractions usually
  2415. are crystal clear, and and reflections look unrealistically crisp.
  2416. The shadows have sharp edges. Although this is good for generating
  2417. nifty pictures, it is not realistic. In the real life, shadows don't
  2418. have these sharp edges, and you cannot comb your hair using a
  2419. teaspoon as a mirror.
  2420.  
  2421.    Let's take a closer look at this spoon. The surface of this spoon
  2422. is scratched and bumped. Indeed, if you would look at the surface
  2423. through a microscope you'd see a pretty wobbly surface. So not all of
  2424. the light falling on the metallic surface will be reflected in the
  2425. same direction.
  2426.  
  2427.    Since light works bidirectional, this also means that eyerays cast
  2428. onto the surface don't generate one single reflection ray, but a huge
  2429. number of "small" reflection rays, all in slightly different
  2430. directions.  The light of a reflection we're searching for is the sum
  2431. of all these tiny contributions from various directions.
  2432.  
  2433.    Mathematically speaking: the intensity I of the incoming light at a
  2434. point P, is a function of the direction D from which the light is
  2435. coming, and of the location on the surface (P), so
  2436.  
  2437.        I = I(D, P)
  2438.  
  2439.    Because the surface is bumped, the light coming from a direction D
  2440. will be reflected in P for percentage dependent on: D, the
  2441. "theoretical" reflection direction R, and the surface normal N at P. 
  2442. So the reflection percentage r is:
  2443.  
  2444.      r (D, N, R)
  2445.  
  2446.    So what we will be seeing of a reflected light from a certain
  2447. direction is
  2448.  
  2449.      r (D, N, R) * I (D)
  2450.  
  2451.    And the total reflection is the sum of all these tiny bits:
  2452.  
  2453.  
  2454.      total  =        Integral            I(D)*r(D,N,R)
  2455.      
  2456.                  all directions D
  2457.  
  2458.    Of course in practical situations, this mathematical descriptions
  2459. isn't gonna help us. However, we could aproximate the above integral
  2460. by carefully choosing a few (say 16) directions D, and summing all
  2461. contributions:
  2462.  
  2463.  
  2464.      total = 1/16       Sum            I(D) * r(D,N,R)
  2465.            ~
  2466.                   some directions D
  2467.  
  2468.    This is the process which is modeled via "distribution" raytracing:
  2469. instead of one, multiple reflection rays are spawned off the surface
  2470. of our spoon. In effect, we are taking discrete samples of the "real"
  2471. reflection I*r, which is a continuous function.
  2472.  
  2473.    In Rayce penumbra, and fuzzy refraction is also modelled in this
  2474. way. There is one caveat: If we would spawn N rays at each
  2475. intersection point for determining the fuzzy reflection, refraction
  2476. or shadow, then we would be tracing O((2N)^recursion_depth) rays. If
  2477. we'd have complex scenes, rendering would take a really LONG time. So
  2478. instead Rayce shoots a fixed number of eyerays. All these eyerays are
  2479. sampled differently at each intersection point (this is called
  2480. multidimensional sampling.  Sounds good, huh? :-).
  2481.  
  2482.    At present, the rays are sampled from a jittered rectangular
  2483. uniform grid, and results are modulated with a filter
  2484.  
  2485.    Of course, you could do the sampling with one eyeray only. But
  2486. then the blur won't be so good since Rayce evaluates the color of the
  2487. ray with only one try.  The more samples the better, but if you use a
  2488. higher resolution, then less samples per pixel suffice.
  2489.  
  2490.    `refl_diffuse' and `refr_diffuse' produce non-sharp reflections
  2491. and refractions.  The argument is in degrees.  Anything less than 15
  2492. or 20 is good, altough the closer to zero the better you can see it.
  2493.  
  2494.    `radius' produce lights that cast soft shadows
  2495.  
  2496.    Focal blur hasn't been done (yet).
  2497.  
  2498.  
  2499. Intersecting hyperspheres with rays.
  2500. *************************************
  2501.  
  2502.    [submitted to RTN, april 94 issue]
  2503.  
  2504.    In raytracing, we're interested in intersecting rays (halflines)
  2505. with various shapes. One of these shapes is the hypersphere. It is
  2506. defined by this equation:
  2507.  
  2508.      |x_1|^q + ... + |x_n|^q = 1    q > 1, (x_1, ..., x_n)in R^n
  2509.  
  2510.    if we define the q-norm by
  2511.  
  2512.      ||x|| = |x_1|^q + ... + |x_n|^q         (q > 1)
  2513.  
  2514.    then this shape can be written easily as
  2515.  
  2516.      ||x|| = 1.
  2517.  
  2518.    Our problem is to find t such that
  2519.  
  2520.      || td + p || = 1
  2521.  
  2522.    for given d,p in R^n This problem can be split in two subproblems:
  2523. determining existence of a solution, and finding the solution itself.
  2524.  
  2525.    When |t| tends to infinity, then ||td+p|| also becomes large.  We
  2526. can therefore restrict ourselves to investigating the behaviour of the
  2527. function ||x(t)|| on an interval [l, h].  This is a compact set, and
  2528. since ||x|| is always positive, it must have a global minimum, for
  2529. some t_0, with ||x(t_0)|| >= 0. If this minimum is larger than 1, then
  2530. ||x(t)|| will be even bigger for all other t; if we find that the
  2531. minimum is larger than one, we can stop searching immediately. On the
  2532. other hand, if the minimum is less than one, a t_0 must exist for
  2533. which ||x(t_0)|| = 1 exactly.  This follows from the fact that
  2534. ||x(t)|| is continuous and ||x(t)|| goes to infinity for t going to
  2535. infinity.
  2536.  
  2537.    So now we have to find the minimum of ||x(t)||. A natural line of
  2538. work is setting the derivative to zero. This has problems. If the
  2539. derivative is zero, this only an indication, not a guarantee that a
  2540. function has in fact got a minimum. For justifying this approach we
  2541. need to take closer look at ||x(t)||.
  2542.  
  2543.    We remind that a convex function on a set S, is a function f for
  2544. which
  2545.  
  2546.      f(A t_0 + (1-A)t_1) <= A f(t_0) + (1-A)f(t_1)      0 < A < 1,
  2547.  
  2548.    for all t_0, t_1 in S, i.e. if you consider f on an interval
  2549. [t_0,t_1], then f is smaller than its linear interpolation on t_0 and
  2550. t_1. If we have strict inequality in the definition, we usually say
  2551. that f is strictly convex.
  2552.  
  2553.    The function x -> |x|^q (x in R) is convex for q >= 1 and if q > 1
  2554. we get strict convexity. The function t -> at+b (a,b in R) is convex.
  2555. It can be proved that the composition of a convex function and a
  2556. strictly convex one is also strictly convex. The sum of two convex
  2557. functions will also be convex. Therefore
  2558.  
  2559.      t -> |td_1 + p_1|^q + |td_2 + p_2|^q + ... + |td_n + p_n|^q    (q > 1)
  2560.  
  2561.    written shortly as t -> ||td + p|| is a strictly convex function
  2562. on R
  2563.  
  2564.    LEMMA. A strictly convex differentiable function has an strictly
  2565. increasing derivative.
  2566.  
  2567.    For a proof, check out any good book on real analysis.  As a
  2568. consequence of this lemma, such a function must have a positive
  2569. second derivative, and it can have no more than one local minimum in
  2570. R.
  2571.  
  2572.    So if we have ||x(t)||' is zero, we automatically have found the
  2573. minimum. Since we already know that a minimum always exists, it should
  2574. always be possible to find such a point.
  2575.  
  2576.    This explanation justifies the following algorithm for finding an
  2577. intersection with a superquadric:
  2578.  
  2579.   1. Find an interval which encloses the intersection, e.g. the
  2580.      intersection of the ray with a unit cube. If it misses the cube,
  2581.      then exit, there will be no intersection.
  2582.  
  2583.   2. Using bisection, find the minimum of || td + p ||, ie the zero
  2584.      of its derivative (grad ||x|| , d).  In this case, we have
  2585.  
  2586.           grad ||x|| = (q sgn (x_1) |x_1|^(q-1), .. , q sgn(x_n) |x_n|^(q-1) ).
  2587.  
  2588.      Here you should take x = td+p.  ( . , .) is my favourite
  2589.      notation for the dot product.
  2590.  
  2591.   3. Is this minimum bigger than one? Exit, there is no intersection.
  2592.  
  2593.   4. In 1. we've found two points for which ||td + p|| > 1 holds. Now
  2594.      we have a t for which ||td + p|| < 1 . Using bisection we're able
  2595.      to find the two ts for which ||td + p|| = 1.
  2596.  
  2597.    There are a lot of tweaks possible, but this is the basic
  2598. algorithm. Here are some straightforward optimisations and extensions:
  2599.  
  2600.    In step one, you could use a better algorithm than bisection, eg.
  2601. Regula Falsi.
  2602.  
  2603.    * In step four, you can use Newton-Raphson's method, the convexity
  2604.      of is a sufficient condition for convergence. Use the
  2605.      intersections found in step 1. as starting points. Since ||x||
  2606.      can be easily calculated from grad ||x||, this is pretty
  2607.      efficient: ||x|| = (grad ||x||, v) and v = (1/q)(x_1, ..., x_n)
  2608.  
  2609.    * In raytracing, we're only interested in the least positive
  2610.      solution, this allows you to get away cheaply if the starting
  2611.      point of the ray is inside the shape.
  2612.  
  2613.    * The algorithm only relies on the convexity and the existence of a
  2614.      minimum of ||td + p||. This algorithm therefore generalizes to a
  2615.      whole class of shapes, formed by a function f:R^n -> R, f(x) =
  2616.      1, with f(td+p) convex, and minimal for some t = t_0. An obvious
  2617.      choice for f would be
  2618.  
  2619.           x_1^(p_1) + x_2^(p_2) + \dots + x_n^(p_n)         (p_i > 1)
  2620.  
  2621.      leading to hypercylinders.
  2622.  
  2623.    * Example: In 3D space, |x|^2 + |y|^2 + |z|^4 = 1 defines a
  2624.      cylinder with rounded edges parallel to the Z-axis.
  2625.  
  2626.    An implementation of this algorithm can be found in Rayce, a
  2627. raytracer by Y.T. It is available from `wuarchive.wustl.edu:
  2628. /graphics/graphics/ray/rayce/'. Rayce has the original TeX document
  2629. included
  2630.  
  2631.  
  2632. Concept Index
  2633. **************
  2634.  
  2635. * Menu:
  2636.  
  2637. * :                                     Compiling.
  2638. * -DDEBUG:                              Invoking Rayce.
  2639. * Algebraic surfaces:                   Primitive Shapes.
  2640. * Algebraic surfaces:                   Primitive Shapes.
  2641. * Ambient:                              Textures.
  2642. * Angle:                                Textures.
  2643. * Black Dots:                           Primitive Shapes.
  2644. * Bounding shapes:                      Objects.
  2645. * Box:                                  Primitive Shapes.
  2646. * Bugs:                                 Bugs.
  2647. * Camera:                               Camera.
  2648. * Clipping Shapes:                      Objects.
  2649. * Colors:                               Basic stuff.
  2650. * compatibility, POV:                   POV compatibility.
  2651. * Compatibility, POV:                   Textures.
  2652. * Composites:                           Objects.
  2653. * Conic extrusion:                      Non-Primitive Shapes.
  2654. * Copyright:                            Copying.
  2655. * Credits:                              Credits.
  2656. * Csg:                                  Non-Primitive Shapes.
  2657. * Csg Part:                             Non-Primitive Shapes.
  2658. * Cylinder:                             Primitive Shapes.
  2659. * Debugging:                            Invoking Rayce.
  2660. * Declare:                              Special Commands.
  2661. * Define:                               Special Commands.
  2662. * Diffuse:                              Textures.
  2663. * Discs:                                Primitive Shapes.
  2664. * Distribution raytracing:              Distribution Raytracing.
  2665. * Distribution raytracing:              Objects.
  2666. * Distribution raytracing:              Options.
  2667. * Extrusion:                            Non-Primitive Shapes.
  2668. * features:                             Introduction.
  2669. * Float:                                Basic stuff.
  2670. * Gloss:                                Textures.
  2671. * High order surfaces:                  Primitive Shapes.
  2672. * Imagemapping:                         Textures.
  2673. * Including files:                      Special Commands.
  2674. * Index of refraction:                  Textures.
  2675. * Input:                                Input Language.
  2676. * Intersection:                         Non-Primitive Shapes.
  2677. * Introduction:                         Introduction.
  2678. * Inverse:                              Non-Primitive Shapes.
  2679. * Invoking:                             Invoking Rayce.
  2680. * Ior:                                  Textures.
  2681. * Known bugs:                           Bugs.
  2682. * Language:                             Input Language.
  2683. * Light intensity:                      Primitive Shapes.
  2684. * Light source:                         Primitive Shapes.
  2685. * Microfacets:                          Textures.
  2686. * Missing Features:                     Introduction.
  2687. * Motion Blur:                          Objects.
  2688. * Non-Primitive Shapes:                 Non-Primitive Shapes.
  2689. * Options:                              Options.
  2690. * Options:                              Invoking Rayce.
  2691. * Patch:                                Primitive Shapes.
  2692. * Plane:                                Primitive Shapes.
  2693. * Polygon:                              Primitive Shapes.
  2694. * Polynomial surfaces:                  Primitive Shapes.
  2695. * Polynomial surfaces:                  Primitive Shapes.
  2696. * Porting scenes:                       POV compatibility.
  2697. * POV:                                  POV compatibility.
  2698. * Primitives:                           Primitive Shapes.
  2699. * Programming:                          Technical Stuff.
  2700. * Quadric:                              Primitive Shapes.
  2701. * Reflection:                           Textures.
  2702. * Refraction:                           Textures.
  2703. * Rotate:                               Non-Primitive Shapes.
  2704. * Rotate:                               Primitive Shapes.
  2705. * Roughness:                            Textures.
  2706. * Scale:                                Non-Primitive Shapes.
  2707. * Scale:                                Primitive Shapes.
  2708. * Shadow Tolerance:                     Options.
  2709. * Smooth triangle:                      Primitive Shapes.
  2710. * Special Commands:                     Special Commands.
  2711. * Specular:                             Textures.
  2712. * Sphere:                               Primitive Shapes.
  2713. * Superquadric:                         Primitive Shapes.
  2714. * Surface characteristics:              Textures.
  2715. * Tech details:                         Technical Stuff.
  2716. * Texture Hierarchy:                    Textures.
  2717. * Texturemapping:                       Textures.
  2718. * Textures:                             Textures.
  2719. * Tolerance:                            Options.
  2720. * Torus:                                Primitive Shapes.
  2721. * Transformations:                      Non-Primitive Shapes.
  2722. * Transformations:                      Primitive Shapes.
  2723. * Translate:                            Non-Primitive Shapes.
  2724. * Translate:                            Primitive Shapes.
  2725. * Translucency:                         Textures.
  2726. * Triangle:                             Primitive Shapes.
  2727. * Union:                                Non-Primitive Shapes.
  2728. * Vectors:                              Basic stuff.
  2729. * Zits:                                 Primitive Shapes.
  2730.  
  2731.